我試圖創建一個目錄和子目錄,並將文件從一個位置複製到另一個位置。下面的代碼有效,但如果有子目錄,它不會創建父目錄(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));
}
}
Idk如果已經有一個簡單的peasy庫函數,但是您可以遞歸地檢查子文件夾並複製每個文件夾。 – Amicable 2013-02-28 16:12:22
請檢查http://stackoverflow.com/questions/58744/best-way-to-copy-the-entire-contents-of-a-directory-in-c-sharp – 2013-02-28 16:13:33