我在 MSDN 上找到了这个解决方案,它可以将文件从一个目录复制到另一个目录,但它只从主目录复制,而不是从子目录复制。
如何修改此解决方案以便它也复制所有文件夹及其内容?
string StartDirectory = @"C:\Users\...";
string EndDirectory = @"C:\Users\....";
//Creates all of the directories and sub-directories
foreach (string dirPath in Directory.GetDirectories(StartDirectory, "*", SearchOption.AllDirectories))
Directory.CreateDirectory(dirPath.Replace(StartDirectory, EndDirectory));
foreach (string filename in Directory.EnumerateFiles(StartDirectory))
{
using (FileStream SourceStream = File.Open(filename, FileMode.Open))
{
using (FileStream DestinationStream = File.Create(EndDirectory + filename.Substring(filename.LastIndexOf('\\'))))
{
await SourceStream.CopyToAsync(DestinationStream);
}
}
}