語言

File.Copy 方法

定義

將現有的檔案複製到新的檔案。

多載

名稱 Description
Copy(String, String)

將現有的檔案複製到新的檔案。 不允許覆寫同名檔案。

Copy(String, String, Boolean)

將現有的檔案複製到新的檔案。 允許覆寫同名檔案。

Copy(String, String)

來源:
File.cs
來源:
File.cs
來源:
File.cs
來源:
File.cs
來源:
File.cs

將現有的檔案複製到新的檔案。 不允許覆寫同名檔案。

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)

參數

sourceFileName
String

要複製的檔案。

destFileName
String

目的地檔案的名稱。 這不能是目錄或現有檔案。

例外狀況

來電者沒有所需的權限。

sourceFileName 或 destFileName 為零長度字串、僅有空白,或包含一個或多個無效字元。 你可以用這個 GetInvalidPathChars() 方法查詢無效字元。

-或-

sourceFileName 或 destFileName 指定目錄。

sourceFileName 或 destFileName 為 null。

指定的路徑、檔名或兩者都超過系統定義的最大長度。

或 sourceFileName 中指定的destFileName路徑無效(例如,該路徑位於未映射的磁碟機上)。

sourceFileName 未被找到。

destFileName 存在。

-或-

發生了 I/O 錯誤。

sourceFileName 或 destFileName 格式無效。

範例

以下範例將檔案複製到 C:\archives\2008 備份資料夾。 它使用該方法的 Copy 兩個過載方式如下:

  • 它首先利用 File.Copy(String, String) 方法過載來複製文字(.txt)檔案。 程式碼顯示這種過載不允許覆蓋已複製的檔案。

  • 接著它會利用 File.Copy(String, String, Boolean) 方法超載來複製圖片(.jpg 檔案)。 程式碼顯示這種過載確實允許覆蓋已複製的檔案。

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

備註

此方法等同 Copy(String, String, Boolean) 於參數 overwrite 設為 false的方法過載。

和sourceFileName參數destFileName可指定相對或絕對路徑資訊。 相對路徑資訊會被解讀為相對於目前工作目錄的相對於。 要取得目前的工作目錄,請參閱該方法。Directory.GetCurrentDirectory 此方法不支援參數中的萬用字元。

原始檔案的屬性會保留在複製的檔案中。

另請參閱

適用於

Copy(String, String, Boolean)

來源:
File.cs
來源:
File.cs
來源:
File.cs
來源:
File.cs
來源:
File.cs

將現有的檔案複製到新的檔案。 允許覆寫同名檔案。

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)

參數

sourceFileName
String

要複製的檔案。

destFileName
String

目的地檔案的名稱。 這不可能是目錄。

overwrite
Boolean

true如果目標檔案已經存在,是否應該被替換;否則,。 false

例外狀況

來電者沒有所需的權限。

-或-

destFileName 是唯讀的。

-或-

overwrite 是 true, destFileName 存在且是隱藏,但 sourceFileName 又不隱藏。

sourceFileName 或 destFileName 為零長度字串、僅有空白,或包含一個或多個無效字元。 你可以用這個 GetInvalidPathChars() 方法查詢無效字元。

-或-

sourceFileName 或 destFileName 指定目錄。

sourceFileName 或 destFileName 為 null。

指定的路徑、檔名或兩者都超過系統定義的最大長度。

或 sourceFileName 中指定的destFileName路徑無效(例如,該路徑位於未映射的磁碟機上)。

sourceFileName 未被找到。

destFileName 存在且 overwrite 為 false。

-或-

發生了 I/O 錯誤。

sourceFileName 或 destFileName 格式無效。

範例

以下範例將檔案複製到 C:\archives\2008 備份資料夾。 它使用該方法的 Copy 兩個過載方式如下:

  • 它首先利用 File.Copy(String, String) 方法過載來複製文字(.txt)檔案。 程式碼顯示這種過載不允許覆蓋已複製的檔案。

接著它會利用 File.Copy(String, String, Boolean) 方法超載來複製圖片(.jpg 檔案)。 程式碼顯示這種過載確實允許覆蓋已複製的檔案。

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

備註

和sourceFileName參數destFileName可指定相對或絕對路徑資訊。 相對路徑資訊會被解讀為相對於目前工作目錄的相對於。 此方法不支援參數中的萬用字元。

原始檔案的屬性會保留在複製的檔案中。

關於常見 I/O 任務的清單,請參見 Common I/O 任務。

另請參閱

適用於