可能是重复问题:
在C#中复制整个目录的最佳方法
我想在.NET中将文件夹及其所有子文件夹和文件从一个位置复制到另一个位置。有没有比遍历目录树更简单、更好或更快的方法?
我看到System.IO.File类上的Copy方法,但想知道是否有比遍历目录树更简单、更好或更快的方法。
可能是重复问题:
在C#中复制整个目录的最佳方法
我想在.NET中将文件夹及其所有子文件夹和文件从一个位置复制到另一个位置。有没有比遍历目录树更简单、更好或更快的方法?
我看到System.IO.File类上的Copy方法,但想知道是否有比遍历目录树更简单、更好或更快的方法。
嗯,这里有Steve提到的VisualBasic.dll实现,还有我自己用过的东西。
private static void CopyDirectory(string sourcePath, string destPath)
{
if (!Directory.Exists(destPath))
{
Directory.CreateDirectory(destPath);
}
foreach (string file in Directory.GetFiles(sourcePath))
{
string dest = Path.Combine(destPath, Path.GetFileName(file));
File.Copy(file, dest);
}
foreach (string folder in Directory.GetDirectories(sourcePath))
{
string dest = Path.Combine(destPath, Path.GetFileName(folder));
CopyDirectory(folder, dest);
}
}
Michal Talaga在他的文章中提到以下内容:
然而,基于File.Copy()
和Directory.CreateDirectory()
的递归实现已足够满足大多数基本需求。
如果没有更好的方法...也许可以使用Process.Start
来启动robocopy.exe
?