System.IO.File.Copy 方法 (String, String)

方法描述

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

语法定义(C# System.IO.File.Copy 方法 (String, String) 的用法)

public static void Copy(
	string sourceFileName,
	string destFileName
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
sourceFileName System-String 要复制的文件。
destFileName System-String 目标文件的名称。它不能是一个目录或现有文件。
返回值 void

提示和注释

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

sourceFileName 和 destFileName 参数可以指定相对或绝对路径信息。 相对路径信息被解释为相对于当前工作目录。 若要获取当前工作目录,请参见 Directory.GetCurrentDirectory 方法。

System.IO.File.Copy 方法 (String, String)例子

该代码演示了此重载不允许覆盖已复制的文件。

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

异常

异常 异常描述
UnauthorizedAccessException 调用方没有所要求的权限。
ArgumentException
  • sourceFileName 或 destFileName 是一个零长度字符串,仅包含空白或者包含一个或多个由 InvalidPathChars 定义的无效字符。
  • sourceFileName 或 destFileName 指定目录。
ArgumentNullException sourceFileName 或 destFileName 为 null。
PathTooLongException 指定的路径、文件名或者两者都超出了系统定义的最大长度。 例如,在基于 Windows 的平台上,路径必须小于 248 个字符,文件名必须小于 260 个字符。
DirectoryNotFoundException 在 sourceFileName 或 destFileName 中指定的路径无效(例如,它位于未映射的驱动器上)。
FileNotFoundException 未找到 sourceFileName。
IOException
  • destFileName 存在。
  • 出现 I/O 错误。
NotSupportedException sourceFileName 或 destFileName 的格式无效。

命名空间

namespace: System.IO

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。