2011-11-27 60 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更有用。

相关问题