File.Copy 方法

定义

将现有文件复制到新文件。

重载

Copy(String, String)

将现有文件复制到新文件。 不允许覆盖同名文件。

Copy(String, String, Boolean)

将现有文件复制到新文件。 允许覆盖同名文件。

Copy(String, String)

Source:
File.cs
Source:
File.cs
Source:
File.cs

将现有文件复制到新文件。 不允许覆盖同名文件。

C#
public static void Copy (string sourceFileName, string destFileName);

参数

sourceFileName
String

要复制的文件。

destFileName
String

目标文件的名称。 这不能是目录或现有文件。

例外

调用方没有所需的权限。

sourceFileNamedestFileName 是长度为零的字符串,仅包含空格,或包含一个或多个无效字符。 可以使用 GetInvalidPathChars() 方法查询无效字符。

-或-

sourceFileNamedestFileName 指定目录。

sourceFileNamedestFileNamenull

指定的路径、文件名或两者都超过了系统定义的最大长度。

sourceFileNamedestFileName 中指定的路径无效(例如,它位于未映射的驱动器上)。

找不到 sourceFileName

destFileName 存在。

-或-

发生 I/O 错误。

sourceFileNamedestFileName 格式无效。

示例

以下示例将文件复制到 C:\archives\2008 备份文件夹。 它使用 Copy 方法的两个重载,如下所示:

  • 它首先使用 File.Copy(String, String) 方法重载复制文本(.txt)文件。 该代码演示此重载不允许覆盖已复制的文件。

  • 然后,它使用 File.Copy(String, String, Boolean) 方法重载复制图片(.jpg 文件)。 该代码演示此重载确实允许覆盖已复制的文件。

C#
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);
}

注解

此方法等效于 Copy(String, String, Boolean) 方法重载,overwrite 参数设置为 false

sourceFileNamedestFileName 参数可以指定相对路径或绝对路径信息。 相对路径信息解释为相对于当前工作目录。 若要获取当前工作目录,请参阅 Directory.GetCurrentDirectory 方法。 此方法不支持参数中的通配符。

原始文件的属性保留在复制的文件中。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Copy(String, String, Boolean)

Source:
File.cs
Source:
File.cs
Source:
File.cs

将现有文件复制到新文件。 允许覆盖同名文件。

C#
public static void Copy (string sourceFileName, string destFileName, bool overwrite);

参数

sourceFileName
String

要复制的文件。

destFileName
String

目标文件的名称。 这不能是目录。

overwrite
Boolean

如果目标文件已存在,则 true 替换目标文件;否则,false

例外

调用方没有所需的权限。

-或-

destFileName 为只读。

-或-

overwrite truedestFileName 存在且隐藏,但 sourceFileName 不隐藏。

sourceFileNamedestFileName 是长度为零的字符串,仅包含空格,或包含一个或多个无效字符。 可以使用 GetInvalidPathChars() 方法查询无效字符。

-或-

sourceFileNamedestFileName 指定目录。

sourceFileNamedestFileNamenull

指定的路径、文件名或两者都超过了系统定义的最大长度。

sourceFileNamedestFileName 中指定的路径无效(例如,它位于未映射的驱动器上)。

找不到 sourceFileName

destFileName 存在,overwritefalse

-或-

发生 I/O 错误。

sourceFileNamedestFileName 格式无效。

示例

以下示例将文件复制到 C:\archives\2008 备份文件夹。 它使用 Copy 方法的两个重载,如下所示:

  • 它首先使用 File.Copy(String, String) 方法重载复制文本(.txt)文件。 该代码演示此重载不允许覆盖已复制的文件。

然后,它使用 File.Copy(String, String, Boolean) 方法重载复制图片(.jpg 文件)。 该代码演示此重载确实允许覆盖已复制的文件。

C#
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);
}

注解

sourceFileNamedestFileName 参数可以指定相对路径或绝对路径信息。 相对路径信息解释为相对于当前工作目录。 此方法不支持参数中的通配符。

原始文件的属性保留在复制的文件中。

有关常见 I/O 任务的列表,请参阅 常见 I/O 任务

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0