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
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
此方法等效于
Copy(String, String, Boolean)
参数设置为
false
的方法重载
overwrite
。
sourceFileName
和
destFileName
参数可以指定相对或绝对路径信息。 相对路径信息被解释为相对于当前工作目录。 若要获取当前工作目录,请参阅
Directory.GetCurrentDirectory
方法。 此方法不支持参数中的通配符。
原始文件的属性保留在复制的文件中。
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
sourceFileName
和
destFileName
参数可以指定相对或绝对路径信息。 相对路径信息被解释为相对于当前工作目录。 此方法不支持参数中的通配符。
原始文件的属性保留在复制的文件中。
有关常见 I/O 任务的列表,请参阅
常见 I/O 任务
。