File.Copy Methode

Definitie

Hiermee kopieert u een bestaand bestand naar een nieuw bestand.

Overloads

Name Description
Copy(String, String)

Hiermee kopieert u een bestaand bestand naar een nieuw bestand. Het overschrijven van een bestand met dezelfde naam is niet toegestaan.

Copy(String, String, Boolean)

Hiermee kopieert u een bestaand bestand naar een nieuw bestand. Het overschrijven van een bestand met dezelfde naam is toegestaan.

Copy(String, String)

Bron:
File.cs
Bron:
File.cs
Bron:
File.cs
Bron:
File.cs
Bron:
File.cs

Hiermee kopieert u een bestaand bestand naar een nieuw bestand. Het overschrijven van een bestand met dezelfde naam is niet toegestaan.

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)

Parameters

sourceFileName
String

Het bestand dat moet worden gekopieerd.

destFileName
String

De naam van het doelbestand. Dit kan geen map of een bestaand bestand zijn.

Uitzonderingen

De beller heeft niet de vereiste machtiging.

sourceFileName of destFileName is een tekenreeks met lengte nul, bevat alleen witruimte of bevat een of meer ongeldige tekens. U kunt een query uitvoeren op ongeldige tekens met behulp van de GetInvalidPathChars() methode.

– of –

sourceFileName of destFileName geeft een map op.

sourceFileName of destFileName is null.

Het opgegeven pad, de bestandsnaam of beide overschrijden de door het systeem gedefinieerde maximumlengte.

Het pad dat is opgegeven in sourceFileName of destFileName is ongeldig (bijvoorbeeld op een niet-toegewezen station).

sourceFileName is niet gevonden.

destFileName Bestaat.

– of –

Er is een I/O-fout opgetreden.

sourceFileName of destFileName heeft een ongeldige indeling.

Voorbeelden

In het volgende voorbeeld worden bestanden gekopieerd naar de back-upmap C:\archives\2008. Hierbij worden de twee overbelastingen van de Copy methode als volgt gebruikt:

  • Eerst wordt de overbelasting van de File.Copy(String, String) methode gebruikt om tekstbestanden (.txt) te kopiëren. De code laat zien dat deze overbelasting geen overschrijven van bestanden toestaat die al zijn gekopieerd.

  • Vervolgens wordt de overbelasting van de File.Copy(String, String, Boolean) methode gebruikt om afbeeldingen (.jpg bestanden) te kopiëren. De code laat zien dat deze overbelasting het overschrijven van bestanden toestaat die al zijn gekopieerd.

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

Opmerkingen

Deze methode is gelijk aan de overbelasting van de Copy(String, String, Boolean) methode waarbij de overwrite parameter is ingesteld op false.

De sourceFileName en destFileName parameters kunnen relatieve of absolute padgegevens opgeven. Relatieve padinformatie wordt geïnterpreteerd als relatief ten opzichte van de huidige werkmap. Zie Directory.GetCurrentDirectory de methode om de huidige werkmap te verkrijgen. Deze methode biedt geen ondersteuning voor jokertekens in de parameters.

De kenmerken van het oorspronkelijke bestand worden bewaard in het gekopieerde bestand.

Zie ook

Van toepassing op

Copy(String, String, Boolean)

Bron:
File.cs
Bron:
File.cs
Bron:
File.cs
Bron:
File.cs
Bron:
File.cs

Hiermee kopieert u een bestaand bestand naar een nieuw bestand. Het overschrijven van een bestand met dezelfde naam is toegestaan.

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)

Parameters

sourceFileName
String

Het bestand dat moet worden gekopieerd.

destFileName
String

De naam van het doelbestand. Dit kan geen map zijn.

overwrite
Boolean

true als het doelbestand moet worden vervangen als het al bestaat; anders, false.

Uitzonderingen

De beller heeft niet de vereiste machtiging.

– of –

destFileName is alleen-lezen.

– of –

overwrite is true, destFileName bestaat en is verborgen, maar sourceFileName is niet verborgen.

sourceFileName of destFileName is een tekenreeks met lengte nul, bevat alleen witruimte of bevat een of meer ongeldige tekens. U kunt een query uitvoeren op ongeldige tekens met behulp van de GetInvalidPathChars() methode.

– of –

sourceFileName of destFileName geeft een map op.

sourceFileName of destFileName is null.

Het opgegeven pad, de bestandsnaam of beide overschrijden de door het systeem gedefinieerde maximumlengte.

Het pad dat is opgegeven in sourceFileName of destFileName is ongeldig (bijvoorbeeld op een niet-toegewezen station).

sourceFileName is niet gevonden.

destFileName bestaat en overwrite is false.

– of –

Er is een I/O-fout opgetreden.

sourceFileName of destFileName heeft een ongeldige indeling.

Voorbeelden

In het volgende voorbeeld worden bestanden gekopieerd naar de back-upmap C:\archives\2008. Hierbij worden de twee overbelastingen van de Copy methode als volgt gebruikt:

  • Eerst wordt de overbelasting van de File.Copy(String, String) methode gebruikt om tekstbestanden (.txt) te kopiëren. De code laat zien dat deze overbelasting geen overschrijven van bestanden toestaat die al zijn gekopieerd.

Vervolgens wordt de overbelasting van de File.Copy(String, String, Boolean) methode gebruikt om afbeeldingen (.jpg bestanden) te kopiëren. De code laat zien dat deze overbelasting het overschrijven van bestanden toestaat die al zijn gekopieerd.

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

Opmerkingen

De sourceFileName en destFileName parameters kunnen relatieve of absolute padgegevens opgeven. Relatieve padinformatie wordt geïnterpreteerd als relatief ten opzichte van de huidige werkmap. Deze methode biedt geen ondersteuning voor jokertekens in de parameters.

De kenmerken van het oorspronkelijke bestand worden bewaard in het gekopieerde bestand.

Zie Algemene I/O-taken voor een lijst met algemene I/O-taken.

Zie ook

Van toepassing op