我正在尝试创建一个目录和子目录,并将文件从一个位置复制到另一个位置。以下代码可以工作,但如果有子目录,则不会创建父目录(10_new)。我正在尝试将"c:\\sourceLoc\\10"中的所有内容(包括子目录)复制到"c:\\destLoc\\10_new"文件夹中。如果"10_new"不存在,则应创建此文件夹。请协助。
string sourceLoc = "c:\\sourceLoc\\10";
string destLoc = "c:\\destLoc\\10_new";
foreach (string dirPath in Directory.GetDirectories(sourceLoc, "*", SearchOption.AllDirectories))
{
Directory.CreateDirectory(dirPath.Replace(sourceLoc, destLoc));
if (Directory.Exists(sourceLoc))
{
//Copy all the files
foreach (string newPath in Directory.GetFiles(sourceLoc, "*.*", SearchOption.AllDirectories))
File.Copy(newPath, newPath.Replace(sourceLoc, destLoc));
}
}