File.Copy Metoda

Definicja

Kopiuje istniejący plik do nowego pliku.

Przeciążenia

Copy(String, String, Boolean)

Kopiuje istniejący plik do nowego pliku. Zastępowanie pliku o tej samej nazwie jest dozwolone.

Copy(String, String)

Kopiuje istniejący plik do nowego pliku. Zastępowanie pliku o tej samej nazwie jest niedozwolone.

Copy(String, String, Boolean)

Źródło:
File.cs
Źródło:
File.cs
Źródło:
File.cs

Kopiuje istniejący plik do nowego pliku. Zastępowanie pliku o tej samej nazwie jest dozwolone.

public:
 static void Copy(System::String ^ sourceFileName, System::String ^ destFileName, bool overwrite);
public static void Copy (string sourceFileName, string destFileName, bool overwrite);
static member Copy : string * string * bool -> unit
Public Shared Sub Copy (sourceFileName As String, destFileName As String, overwrite As Boolean)

Parametry

sourceFileName
String

Plik do skopiowania.

destFileName
String

Nazwa pliku docelowego. Nie może to być katalog.

overwrite
Boolean

true jeśli plik docelowy powinien zostać zastąpiony, jeśli już istnieje; w przeciwnym razie , false.

Wyjątki

Obiekt wywołujący nie posiada wymaganych uprawnień.

-lub-

destFileName jest tylko do odczytu.

-lub-

overwrite jest true, destFileName istnieje i jest ukryty, ale sourceFileName nie jest ukryty.

sourceFileName lub destFileName jest ciągiem o zerowej długości, zawiera tylko białe znaki lub zawiera co najmniej jeden nieprawidłowy znak. Możesz wykonać zapytanie o nieprawidłowe znaki przy użyciu GetInvalidPathChars() metody .

-lub-

sourceFileName lub destFileName określa katalog.

sourceFileName lub destFileName to null.

Podana ścieżka, nazwa pliku lub obie przekraczają maksymalną długość zdefiniowaną przez system.

Ścieżka określona w sourceFileName pliku lub destFileName jest nieprawidłowa (na przykład znajduje się na niemapowanym dysku).

sourceFileName nie znaleziono.

destFileName istnieje i overwrite ma wartość false.

-lub-

Wystąpił błąd we/wy.

sourceFileName lub destFileName jest w nieprawidłowym formacie.

Przykłady

Poniższy przykład kopiuje pliki do folderu kopii zapasowej C:\archives\2008. Używa dwóch przeciążeń Copy metody w następujący sposób:

  • Najpierw używa przeciążenia metody do kopiowania File.Copy(String, String) plików tekstowych (.txt). Kod pokazuje, że to przeciążenie nie zezwala na zastępowanie plików, które zostały już skopiowane.

Następnie używa przeciążenia metody do kopiowania File.Copy(String, String, Boolean) obrazów (.jpg plików). Kod pokazuje, że to przeciążenie zezwala na zastępowanie plików, które zostały już skopiowane.

string sourceDir = @"c:\current";
string backupDir = @"c:\archives\2008";

try
{
    string[] picList = Directory.GetFiles(sourceDir, "*.jpg");
    string[] txtList = Directory.GetFiles(sourceDir, "*.txt");

    // Copy picture files.
    foreach (string f in picList)
    {
        // Remove path from the file name.
        string fName = f.Substring(sourceDir.Length + 1);

        // Use the Path.Combine method to safely append the file name to the path.
        // Will overwrite if the destination file already exists.
        File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName), true);
    }

    // Copy text files.
    foreach (string f in txtList)
    {

        // Remove path from the file name.
        string fName = f.Substring(sourceDir.Length + 1);

        try
        {
            // Will not overwrite if the destination file already exists.
            File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName));
        }

        // Catch exception if the file was already copied.
        catch (IOException copyError)
        {
            Console.WriteLine(copyError.Message);
        }
    }

    // Delete source files that were copied.
    foreach (string f in txtList)
    {
        File.Delete(f);
    }
    foreach (string f in picList)
    {
        File.Delete(f);
    }
}

catch (DirectoryNotFoundException dirNotFound)
{
    Console.WriteLine(dirNotFound.Message);
}
let sourceDir = @"c:\current"
let backupDir = @"c:\archives\2008"

try
    let picList = Directory.GetFiles(sourceDir, "*.jpg")
    let txtList = Directory.GetFiles(sourceDir, "*.txt")

    // Copy picture files.
    for f in picList do
        // Remove path from the file name.
        let fName = f.Substring(sourceDir.Length + 1)

        // Use the Path.Combine method to safely append the file name to the path.
        // Will overwrite if the destination file already exists.
        File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName), true)

    // Copy text files.
    for f in txtList do
        // Remove path from the file name.
        let fName = f.Substring(sourceDir.Length + 1)

        try
            // Will not overwrite if the destination file already exists.
            File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName))

        // Catch exception if the file was already copied.
        with
        | :? IOException as copyError -> printfn $"{copyError.Message}"

    // Delete source files that were copied.
    for f in txtList do
        File.Delete f

    for f in picList do
        File.Delete f

// Catch exception if the file was already copied.
with
| :? DirectoryNotFoundException as dirNotFound -> printfn $"{dirNotFound.Message}"
Dim sourceDir As String = "c:\current"
Dim backupDir As String = "c:\archives\2008"

Try
    Dim picList As String() = Directory.GetFiles(sourceDir, "*.jpg")
    Dim txtList As String() = Directory.GetFiles(sourceDir, "*.txt")

    ' Copy picture files.
    For Each f As String In picList
        'Remove path from the file name.
        Dim fName As String = f.Substring(sourceDir.Length + 1)

        ' Use the Path.Combine method to safely append the file name to the path.
        ' Will overwrite if the destination file already exists.
        File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName), True)
    Next

    ' Copy text files.
    For Each f As String In txtList

        'Remove path from the file name.
        Dim fName As String = f.Substring(sourceDir.Length + 1)

        Try
            ' Will not overwrite if the destination file already exists.
            File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName))

            ' Catch exception if the file was already copied.
        Catch copyError As IOException
            Console.WriteLine(copyError.Message)
        End Try
    Next

    For Each f As String In txtList
        File.Delete(f)
    Next

    For Each f As String In picList
        File.Delete(f)
    Next

Catch dirNotFound As DirectoryNotFoundException
    Console.WriteLine(dirNotFound.Message)
End Try

Uwagi

Parametry sourceFileName i destFileName mogą określać informacje o ścieżce względnej lub bezwzględnej. Informacje o ścieżce względnej są interpretowane jako względne w stosunku do bieżącego katalogu roboczego. Ta metoda nie obsługuje symboli wieloznacznych w parametrach.

Atrybuty oryginalnego pliku są zachowywane w skopiowanych plikach.

Aby uzyskać listę typowych zadań we/wy, zobacz Typowe zadania we/wy.

Zobacz też

Dotyczy

Copy(String, String)

Źródło:
File.cs
Źródło:
File.cs
Źródło:
File.cs

Kopiuje istniejący plik do nowego pliku. Zastępowanie pliku o tej samej nazwie jest niedozwolone.

public:
 static void Copy(System::String ^ sourceFileName, System::String ^ destFileName);
public static void Copy (string sourceFileName, string destFileName);
static member Copy : string * string -> unit
Public Shared Sub Copy (sourceFileName As String, destFileName As String)

Parametry

sourceFileName
String

Plik do skopiowania.

destFileName
String

Nazwa pliku docelowego. Nie może to być katalog lub istniejący plik.

Wyjątki

Obiekt wywołujący nie posiada wymaganych uprawnień.

sourceFileName lub destFileName jest ciągiem o zerowej długości, zawiera tylko białe znaki lub zawiera co najmniej jeden nieprawidłowy znak. Możesz wykonać zapytanie o nieprawidłowe znaki przy użyciu GetInvalidPathChars() metody .

-lub-

sourceFileName lub destFileName określa katalog.

sourceFileName lub destFileName to null.

Podana ścieżka, nazwa pliku lub obie przekraczają maksymalną długość zdefiniowaną przez system.

Ścieżka określona w sourceFileName pliku lub destFileName jest nieprawidłowa (na przykład znajduje się na niemapowanym dysku).

sourceFileName nie znaleziono.

destFileName Istnieje.

-lub-

Wystąpił błąd we/wy.

sourceFileName lub destFileName jest w nieprawidłowym formacie.

Przykłady

Poniższy przykład kopiuje pliki do folderu kopii zapasowej C:\archives\2008. Używa dwóch przeciążeń Copy metody w następujący sposób:

  • Najpierw używa przeciążenia metody do kopiowania File.Copy(String, String) plików tekstowych (.txt). Kod pokazuje, że to przeciążenie nie zezwala na zastępowanie plików, które zostały już skopiowane.

  • Następnie używa przeciążenia metody do kopiowania File.Copy(String, String, Boolean) obrazów (.jpg plików). Kod pokazuje, że to przeciążenie zezwala na zastępowanie plików, które zostały już skopiowane.

string sourceDir = @"c:\current";
string backupDir = @"c:\archives\2008";

try
{
    string[] picList = Directory.GetFiles(sourceDir, "*.jpg");
    string[] txtList = Directory.GetFiles(sourceDir, "*.txt");

    // Copy picture files.
    foreach (string f in picList)
    {
        // Remove path from the file name.
        string fName = f.Substring(sourceDir.Length + 1);

        // Use the Path.Combine method to safely append the file name to the path.
        // Will overwrite if the destination file already exists.
        File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName), true);
    }

    // Copy text files.
    foreach (string f in txtList)
    {

        // Remove path from the file name.
        string fName = f.Substring(sourceDir.Length + 1);

        try
        {
            // Will not overwrite if the destination file already exists.
            File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName));
        }

        // Catch exception if the file was already copied.
        catch (IOException copyError)
        {
            Console.WriteLine(copyError.Message);
        }
    }

    // Delete source files that were copied.
    foreach (string f in txtList)
    {
        File.Delete(f);
    }
    foreach (string f in picList)
    {
        File.Delete(f);
    }
}

catch (DirectoryNotFoundException dirNotFound)
{
    Console.WriteLine(dirNotFound.Message);
}
let sourceDir = @"c:\current"
let backupDir = @"c:\archives\2008"

try
    let picList = Directory.GetFiles(sourceDir, "*.jpg")
    let txtList = Directory.GetFiles(sourceDir, "*.txt")

    // Copy picture files.
    for f in picList do
        // Remove path from the file name.
        let fName = f.Substring(sourceDir.Length + 1)

        // Use the Path.Combine method to safely append the file name to the path.
        // Will overwrite if the destination file already exists.
        File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName), true)

    // Copy text files.
    for f in txtList do
        // Remove path from the file name.
        let fName = f.Substring(sourceDir.Length + 1)

        try
            // Will not overwrite if the destination file already exists.
            File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName))

        // Catch exception if the file was already copied.
        with
        | :? IOException as copyError -> printfn $"{copyError.Message}"

    // Delete source files that were copied.
    for f in txtList do
        File.Delete f

    for f in picList do
        File.Delete f

// Catch exception if the file was already copied.
with
| :? DirectoryNotFoundException as dirNotFound -> printfn $"{dirNotFound.Message}"
Dim sourceDir As String = "c:\current"
Dim backupDir As String = "c:\archives\2008"

Try
    Dim picList As String() = Directory.GetFiles(sourceDir, "*.jpg")
    Dim txtList As String() = Directory.GetFiles(sourceDir, "*.txt")

    ' Copy picture files.
    For Each f As String In picList
        'Remove path from the file name.
        Dim fName As String = f.Substring(sourceDir.Length + 1)

        ' Use the Path.Combine method to safely append the file name to the path.
        ' Will overwrite if the destination file already exists.
        File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName), True)
    Next

    ' Copy text files.
    For Each f As String In txtList

        'Remove path from the file name.
        Dim fName As String = f.Substring(sourceDir.Length + 1)

        Try
            ' Will not overwrite if the destination file already exists.
            File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName))

            ' Catch exception if the file was already copied.
        Catch copyError As IOException
            Console.WriteLine(copyError.Message)
        End Try
    Next

    For Each f As String In txtList
        File.Delete(f)
    Next

    For Each f As String In picList
        File.Delete(f)
    Next

Catch dirNotFound As DirectoryNotFoundException
    Console.WriteLine(dirNotFound.Message)
End Try

Uwagi

Ta metoda jest odpowiednikiem Copy(String, String, Boolean) przeciążenia metody z parametrem ustawionym overwrite na falsewartość .

Parametry sourceFileName i destFileName mogą określać informacje o ścieżce względnej lub bezwzględnej. Informacje o ścieżce względnej są interpretowane jako względne w stosunku do bieżącego katalogu roboczego. Aby uzyskać bieżący katalog roboczy, zobacz metodę Directory.GetCurrentDirectory . Ta metoda nie obsługuje symboli wieloznacznych w parametrach.

Atrybuty oryginalnego pliku są zachowywane w skopiowanych plikach.

Zobacz też

Dotyczy