Bagikan melalui


File.Copy Metode

Definisi

Menyalin file yang sudah ada ke file baru.

Overload

Copy(String, String, Boolean)

Menyalin file yang sudah ada ke file baru. Menimpa file dengan nama yang sama diperbolehkan.

Copy(String, String)

Menyalin file yang sudah ada ke file baru. Menimpa file dengan nama yang sama tidak diperbolehkan.

Copy(String, String, Boolean)

Sumber:
File.cs
Sumber:
File.cs
Sumber:
File.cs

Menyalin file yang sudah ada ke file baru. Menimpa file dengan nama yang sama diperbolehkan.

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)

Parameter

sourceFileName
String

File yang akan disalin.

destFileName
String

Nama file tujuan. Ini tidak boleh berupa direktori.

overwrite
Boolean

true jika file tujuan harus diganti jika sudah ada; jika tidak, false.

Pengecualian

Pemanggil tidak memiliki izin yang diperlukan.

-atau-

destFileName bersifat baca-saja.

-atau-

overwrite adalah true, destFileName ada dan tersembunyi, tetapi sourceFileName tidak tersembunyi.

sourceFileName atau destFileName adalah string panjang nol, hanya berisi spasi kosong, atau berisi satu atau beberapa karakter yang tidak valid. Anda dapat mengkueri karakter yang tidak valid dengan menggunakan GetInvalidPathChars() metode .

-atau-

sourceFileName atau destFileName menentukan direktori.

sourceFileName atau destFileName adalah null.

Jalur yang ditentukan, nama file, atau keduanya melebihi panjang maksimum yang ditentukan sistem.

Jalur yang ditentukan di sourceFileName atau destFileName tidak valid (misalnya, ada pada drive yang tidak dipetakan).

sourceFileName tidak ditemukan.

destFileName ada dan overwrite adalah false.

-atau-

Terjadi kesalahan I/O.

sourceFileName atau destFileName dalam format yang tidak valid.

Contoh

Contoh berikut menyalin file ke folder cadangan C:\archives\2008. Ini menggunakan dua kelebihan beban Copy metode sebagai berikut:

  • Ini pertama-tama File.Copy(String, String) menggunakan metode kelebihan beban untuk menyalin file teks (.txt). Kode menunjukkan bahwa kelebihan beban ini tidak memungkinkan penimpaan file yang sudah disalin.

Kemudian menggunakan File.Copy(String, String, Boolean) metode kelebihan beban untuk menyalin gambar (.jpg file). Kode menunjukkan bahwa kelebihan beban ini memungkinkan penimpaan file yang sudah disalin.

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

Keterangan

Parameter sourceFileName dan destFileName dapat menentukan informasi jalur relatif atau absolut. Informasi jalur relatif ditafsirkan relatif terhadap direktori kerja saat ini. Metode ini tidak mendukung karakter kartubebas dalam parameter.

Atribut file asli dipertahankan dalam file yang disalin.

Untuk daftar tugas I/O umum, lihat Tugas I/O Umum.

Lihat juga

Berlaku untuk

Copy(String, String)

Sumber:
File.cs
Sumber:
File.cs
Sumber:
File.cs

Menyalin file yang sudah ada ke file baru. Menimpa file dengan nama yang sama tidak diperbolehkan.

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)

Parameter

sourceFileName
String

File yang akan disalin.

destFileName
String

Nama file tujuan. Ini tidak boleh berupa direktori atau file yang ada.

Pengecualian

Pemanggil tidak memiliki izin yang diperlukan.

sourceFileName atau destFileName adalah string panjang nol, hanya berisi spasi kosong, atau berisi satu atau beberapa karakter yang tidak valid. Anda dapat mengkueri karakter yang tidak valid dengan menggunakan GetInvalidPathChars() metode .

-atau-

sourceFileName atau destFileName menentukan direktori.

sourceFileName atau destFileName adalah null.

Jalur yang ditentukan, nama file, atau keduanya melebihi panjang maksimum yang ditentukan sistem.

Jalur yang ditentukan di sourceFileName atau destFileName tidak valid (misalnya, ada pada drive yang tidak dipetakan).

sourceFileName tidak ditemukan.

destFileName Ada.

-atau-

Terjadi kesalahan I/O.

sourceFileName atau destFileName dalam format yang tidak valid.

Contoh

Contoh berikut menyalin file ke folder cadangan C:\archives\2008. Ini menggunakan dua kelebihan beban Copy metode sebagai berikut:

  • Ini pertama-tama File.Copy(String, String) menggunakan metode kelebihan beban untuk menyalin file teks (.txt). Kode menunjukkan bahwa kelebihan beban ini tidak memungkinkan penimpaan file yang sudah disalin.

  • Kemudian menggunakan File.Copy(String, String, Boolean) metode kelebihan beban untuk menyalin gambar (.jpg file). Kode menunjukkan bahwa kelebihan beban ini memungkinkan penimpaan file yang sudah disalin.

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

Keterangan

Metode ini setara dengan Copy(String, String, Boolean) metode kelebihan beban dengan parameter yang overwrite diatur ke false.

Parameter sourceFileName dan destFileName dapat menentukan informasi jalur relatif atau absolut. Informasi jalur relatif ditafsirkan relatif terhadap direktori kerja saat ini. Untuk mendapatkan direktori kerja saat ini, lihat Directory.GetCurrentDirectory metode . Metode ini tidak mendukung karakter kartubebas dalam parameter.

Atribut file asli dipertahankan dalam file yang disalin.

Lihat juga

Berlaku untuk