可能重复: Best方式的目录的全部内容复制在C#的
我想从一个位置复制文件夹及其所有子文件夹和文件到另一个.NET。什么是做到这一点的最好方法是什么?
I'd like to copy folder with all its subfolders and file from one location to another in .NET. What's the best way to do this?
我看到有System.IO.File类的复制方法,但不知道是否有比抓取的目录树更容易,更好,或者更快的方式。
I see the Copy method on the System.IO.File class, but was wondering whether there was an easier, better, or faster way than to crawl the directory tree.
嗯,还有的VisualBasic.dll实现,史蒂夫引用,这里的东西,我用。
Well, there's the VisualBasic.dll implementation that Steve references, and here's something that I've used.
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);
}
}
上一篇:从文本使用C#提取所有的电子邮件地址有的、电子邮件地址、文本
下一篇:我应该使用单()或的SingleOrDefault(),如果有机会的元素不会被发现?有机会、元素、发现、SingleOrDefault