2011-11-27 84 views
0

請問C#專家是否可以解決一個簡單問題,由於某種奇怪的原因,我似乎無法解決這個問題?我想在當前目錄中移動多個子文件夾到一個新的目錄,並保持子文件夾名稱,見下圖:將多個子文件夾移動到不同的目錄並保留文件夾名稱

public string currentDirectory = System.Environment.GetEnvironmentVariable("LOCALAPPDATA") + @"\Test\CurrentFolder\"; 

public string newDirectory = System.Environment.GetEnvironmentVariable("LOCALAPPDATA") + @"\Test\NewFolder\"; 

private void btnMoveFolder_Click(object sender, RoutedEventArgs e) 
{ 
    string[] subdirectoryEntries = Directory.GetDirectories(currentDirectory); 
    try 
    { 
     foreach (string subCurrentDirectory in subdirectoryEntries) 
     { 
      Directory.Move(subCurrentDirectory, newDirectory); 

     } 
    } 
    catch (System.Exception) 
    { 
     Log("Problem with moving the directory."); 
    } 
} 

此刻,我只能似乎能夠移動而不是所有的一個文件夾的他們。

任何幫助將不勝感激!

+2

sooo ...你正在使用的代碼是什麼_problem_(除格式:))? – sehe

回答

2

我想您是這樣的:

Directory.Move(subCurrentDirectory, 
    Path.Combine(
     newDirectory, 
     Path.GetFileName(subCurrentDirectory))); 
+0

嗨Sehe,你的代碼工作得很好,謝謝!順便說一句,我的格式有什麼問題? :)我只在晚上編程了10個月,所以任何提示都表示讚賞。謝謝 –

2

嘗試了這一點:

DirectoryInfo subfolder = new DirectoryInfo(@"OLDPATH\DirectoryToMove"); 
subfolder.MoveTo(@"NEWPATH\DirectoryToMove"); 

只要確保您在這兩個新舊文件路徑移動的目錄的名稱。

一般而言DirectoryInfo和FileInfo在大多數情況下比Directory和File更有用。

相關問題