File.Copy Metoda

Definice

Zkopíruje existující soubor do nového souboru.

Přetížení

Copy(String, String, Boolean)

Zkopíruje existující soubor do nového souboru. Přepsání souboru se stejným názvem je povoleno.

Copy(String, String)

Zkopíruje existující soubor do nového souboru. Přepsání souboru se stejným názvem není povoleno.

Copy(String, String, Boolean)

Zdroj:
File.cs
Zdroj:
File.cs
Zdroj:
File.cs

Zkopíruje existující soubor do nového souboru. Přepsání souboru se stejným názvem je povoleno.

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

Soubor, který chcete zkopírovat.

destFileName
String

Název cílového souboru. Nemůže se jednat o adresář.

overwrite
Boolean

truepokud by cílový soubor měl být nahrazen, pokud již existuje; v opačném případě . false

Výjimky

Volající nemá požadované oprávnění.

-nebo-

destFileName je jen pro čtení.

-nebo-

overwrite je true, destFileName existuje a je skrytý, ale sourceFileName není skrytý.

sourceFileName nebo destFileName je řetězec nulové délky, obsahuje pouze prázdné znaky nebo obsahuje jeden nebo více neplatných znaků. Na neplatné znaky se můžete dotazovat pomocí metody .GetInvalidPathChars()

-nebo-

sourceFileName nebo destFileName určuje adresář.

sourceFileName nebo destFileName je null.

Zadaná cesta, název souboru nebo obojí překračují maximální délku definovanou systémem.

Cesta zadaná v sourceFileName nebo destFileName je neplatná (například se nachází na nenamapované jednotce).

sourceFileName nebyla nalezena.

destFileName existuje a overwrite je false.

-nebo-

Došlo k vstupně-výstupní chybě.

sourceFileName nebo destFileName je v neplatném formátu.

Příklady

Následující příklad zkopíruje soubory do záložní složky C:\archives\2008. Používá dvě přetížení Copy metody následujícím způsobem:

  • Nejprve použije File.Copy(String, String) přetížení metody ke kopírování textových souborů (.txt). Kód ukazuje, že toto přetížení neumožňuje přepsání souborů, které již byly zkopírovány.

Pak použije File.Copy(String, String, Boolean) přetížení metody ke kopírování obrázků (.jpg souborů). Kód ukazuje, že toto přetížení umožňuje přepsání souborů, které již byly zkopírovány.

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

Poznámky

Parametry sourceFileName a destFileName mohou určovat relativní nebo absolutní informace o cestě. Informace o relativní cestě jsou vykládány jako relativní k aktuálnímu pracovnímu adresáři. Tato metoda nepodporuje zástupné znaky v parametrech.

Atributy původního souboru se zachovají ve zkopírovaných souborech.

Seznam běžných vstupně-výstupních úloh najdete v tématu Běžné vstupně-výstupní úlohy.

Viz také

Platí pro

Copy(String, String)

Zdroj:
File.cs
Zdroj:
File.cs
Zdroj:
File.cs

Zkopíruje existující soubor do nového souboru. Přepsání souboru se stejným názvem není povoleno.

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

Soubor, který chcete zkopírovat.

destFileName
String

Název cílového souboru. Nemůže se jednat o adresář ani existující soubor.

Výjimky

Volající nemá požadované oprávnění.

sourceFileName nebo destFileName je řetězec nulové délky, obsahuje pouze prázdné znaky nebo obsahuje jeden nebo více neplatných znaků. Na neplatné znaky se můžete dotazovat pomocí metody .GetInvalidPathChars()

-nebo-

sourceFileName nebo destFileName určuje adresář.

sourceFileName nebo destFileName je null.

Zadaná cesta, název souboru nebo obojí překračují maximální délku definovanou systémem.

Cesta zadaná v sourceFileName nebo destFileName je neplatná (například se nachází na nenamapované jednotce).

sourceFileName nebyla nalezena.

destFileName Existuje.

-nebo-

Došlo k vstupně-výstupní chybě.

sourceFileName nebo destFileName je v neplatném formátu.

Příklady

Následující příklad zkopíruje soubory do záložní složky C:\archives\2008. Používá dvě přetížení Copy metody následujícím způsobem:

  • Nejprve použije File.Copy(String, String) přetížení metody ke kopírování textových souborů (.txt). Kód ukazuje, že toto přetížení neumožňuje přepsání souborů, které již byly zkopírovány.

  • Pak použije File.Copy(String, String, Boolean) přetížení metody ke kopírování obrázků (.jpg souborů). Kód ukazuje, že toto přetížení umožňuje přepsání souborů, které již byly zkopírovány.

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

Poznámky

Tato metoda je ekvivalentní Copy(String, String, Boolean) přetížení metody s parametrem nastaveným overwrite na false.

Parametry sourceFileName a destFileName mohou určovat relativní nebo absolutní informace o cestě. Informace o relativní cestě jsou vykládány jako relativní k aktuálnímu pracovnímu adresáři. Pokud chcete získat aktuální pracovní adresář, projděte si metodu Directory.GetCurrentDirectory . Tato metoda nepodporuje zástupné znaky v parametrech.

Atributy původního souboru se zachovají ve zkopírovaných souborech.

Viz také

Platí pro