C# 如何复制文件,覆盖现有文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16556766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I copy files, overwriting existing files?
提问by Jin Yong
Overview
概述
How do I copy all files from one directory to another directory and overwrite all existing same-named files in the target directory with C#?
如何将所有文件从一个目录复制到另一个目录并用 C# 覆盖目标目录中所有现有的同名文件?
I have the following code to copy the files from one directory to another directory...
我有以下代码将文件从一个目录复制到另一个目录...
const string sourceDir = @"C:\AppProject\Smart\SmartStaff\site\document";
const string targetDir = @"C:\AppProject\Smart\ExternalSmartStaff\site\document";
foreach (var file in Directory.GetFiles(sourceDir))
File.Copy(file, Path.Combine(targetDir, Path.GetFileName(file)));
..., but when the target directory already contains a file with the same name as a file in the source directory, it fails with the error System.IO.IOException: The file 'C:\AppProject\Smart\ExternalSmartStaff\site\document\SomeDocument.txt' already exists..
...,但是当目标目录已经包含与源目录中的文件同名的文件时,它会失败并显示错误System.IO.IOException: The file 'C:\AppProject\Smart\ExternalSmartStaff\site\document\SomeDocument.txt' already exists.。
Details
细节
To be clear, given the following directories and files in them...
需要明确的是,鉴于其中的以下目录和文件...
C:\>dir C:\AppProject\Smart\SmartStaff\site\document
...
Directory of C:\AppProject\Smart\SmartStaff\site\document
09/03/2014 06:38 PM <DIR> .
09/03/2014 06:38 PM <DIR> ..
05/25/2014 08:29 PM 44 SomeDocument.txt
05/25/2014 08:32 PM 21 SomeDocument2.txt
05/25/2014 08:36 PM 21 SomeDocument3.txt
05/25/2014 08:43 PM 44 SomeDocument4.txt
4 File(s) 130 bytes
2 Dir(s) 128,326,766,592 bytes free
C:\>dir C:\AppProject\Smart\ExternalSmartStaff\site\document
...
Directory of C:\AppProject\Smart\ExternalSmartStaff\site\document
09/03/2014 06:39 PM <DIR> .
09/03/2014 06:39 PM <DIR> ..
09/03/2014 06:39 PM 26 SomeDocument.txt
09/03/2014 06:39 PM 54 SomeDocument2.txt
2 File(s) 80 bytes
2 Dir(s) 128,326,766,592 bytes free
..., I would like C:\AppProject\Smart\ExternalSmartStaff\site\documentto look like this after the file copy in C#:
...,我想C:\AppProject\Smart\ExternalSmartStaff\site\document在 C# 中复制文件后看起来像这样:
C:\>dir C:\AppProject\Smart\ExternalSmartStaff\site\document
...
Directory of C:\AppProject\Smart\ExternalSmartStaff\site\document
09/03/2014 06:47 PM <DIR> .
09/03/2014 06:47 PM <DIR> ..
05/25/2014 08:29 PM 44 SomeDocument.txt
05/25/2014 08:32 PM 21 SomeDocument2.txt
05/25/2014 08:36 PM 21 SomeDocument3.txt
05/25/2014 08:43 PM 44 SomeDocument4.txt
4 File(s) 130 bytes
2 Dir(s) 128,327,835,648 bytes free
How can I avoid the IOExceptionand accomplish this?
我怎样才能避免IOException并实现这一点?
采纳答案by J0e3gan
Try this:
尝试这个:
const string sourceDir = @"C:\AppProject\Smart\SmartStaff\site\document";
const string targetDir = @"C:\AppProject\Smart\ExternalSmartStaff\site\document";
foreach (var file in Directory.GetFiles(sourceDir))
File.Copy(file, Path.Combine(targetDir, Path.GetFileName(file)), true);
Basically, you just need to call a different overload of File.Copy. MSDN documentationexplains that the third, boolparameter is to indicate whether to overwrite existing files with the files being copied.
基本上,您只需要调用File.Copy. MSDN 文档解释说,第三个bool参数是指示是否用正在复制的文件覆盖现有文件。
回答by Adriaan Stander
Rather use
而是使用
File.Copy Method (String, String, Boolean)
Copies an existing file to a new file. Overwriting a file of the same name is allowed.
将现有文件复制到新文件。允许覆盖同名文件。
Where
在哪里
overwriteType: System.Boolean
true if the destination file can be overwritten; otherwise, false.
覆盖类型:System.Boolean
如果目标文件可以被覆盖,则为 true;否则为假。
回答by cvraman
The File.Copymethod has an overload that allows for overwriting existing files with the same name.
See http://msdn.microsoft.com/en-us/library/9706cfs5.aspx.
该File.Copy方法有一个重载,允许覆盖同名的现有文件。请参阅http://msdn.microsoft.com/en-us/library/9706cfs5.aspx。
回答by Maryam Arshi
This is a solution
这是一个解决方案
foreach (string path in Directory.GetFiles(SourcePath, "*.*", SearchOption.AllDirectories))
File.Copy(path, path.Replace(SourcePath, DestinationPath));

