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)
ArgumentException

sourceFileName destFileName 為零長度字串,只包含空格或包含一或多個無效字元。 您可以使用 GetInvalidPathChars() 方法查詢無效字元。

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";
        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);
                // 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"
        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)
                // 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.
            | :? 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.
    | :? DirectoryNotFoundException as dirNotFound -> printfn $"{dirNotFound.Message}"
    
    Dim sourceDir As String = "c:\current"
    Dim backupDir As String = "c:\archives\2008"
        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)
        ' 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)
                ' 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
        For Each f As String In txtList
            File.Delete(f)
        For Each f As String In picList
            File.Delete(f)
    Catch dirNotFound As DirectoryNotFoundException
        Console.WriteLine(dirNotFound.Message)
    End Try
        	

    sourceFileNamedestFileName 參數可以指定相對或絕對路徑資訊。 相對路徑資訊會解譯為相對於目前的工作目錄。 這個方法不支持參數中的通配符。

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

    如需一般 I/O 工作的清單,請參閱 一般 I/O 工作

    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)
    ArgumentException

    sourceFileNamedestFileName 為零長度字串,只包含空格或包含一或多個無效字元。 您可以使用 GetInvalidPathChars() 方法查詢無效字元。

    sourceFileNamedestFileName 會指定目錄。

    下列範例會將檔案複製到 C:\archives\2008 備份資料夾。 它會使用 方法的兩個 Copy 多載,如下所示:

  • 它會先使用 File.Copy(String, String) 方法多載來複製 (.txt) 檔案的文字。 此程式代碼示範此多載不允許覆寫已複製的檔案。

  • 然後, File.Copy(String, String, Boolean) 它會使用 方法多載來複製圖片 (.jpg 檔案) 。 此程式代碼示範此多載確實允許覆寫已複製的檔案。

    string sourceDir = @"c:\current";
    string backupDir = @"c:\archives\2008";
        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);
                // 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"
        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)
                // 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.
            | :? 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.
    | :? DirectoryNotFoundException as dirNotFound -> printfn $"{dirNotFound.Message}"
    
    Dim sourceDir As String = "c:\current"
    Dim backupDir As String = "c:\archives\2008"
        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)
        ' 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)
                ' 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
        For Each f As String In txtList
            File.Delete(f)
        For Each f As String In picList
            File.Delete(f)
    Catch dirNotFound As DirectoryNotFoundException
        Console.WriteLine(dirNotFound.Message)
    End Try
        	

    這個方法相當於方法多載,Copy(String, String, Boolean)overwrite並將 參數設定為 false

    sourceFileNamedestFileName 參數可以指定相對或絕對路徑資訊。 相對路徑資訊會解譯為相對於目前的工作目錄。 若要取得目前的工作目錄,請參閱 Directory.GetCurrentDirectory 方法。 這個方法不支持參數中的通配符。

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

    即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:https://aka.ms/ContentUserFeedback

    提交並檢視相關的意見反應

  •