2011-02-04 54 views
0

我可以複製多個目錄中的所有文件,但我想要做的是將所有目錄與其中的文件複製,因爲它們是我從哪裏複製而不是把文件放在我的目標文件夾中。這是到目前爲止我的代碼我已經得到了MSDN代碼,但仍然無法讓我的程序與它一起工作

{ 
    string SelectedPath = (string)e.Argument; 
    string sourceDirName; 
    string destDirName; 
    bool copySubDirs; 
    DirectoryCopy(".", SelectedPath, true); 

    DirectoryInfo dir = new DirectoryInfo(sourceDirName); 
    DirectoryInfo[] dirs = dir.GetDirectories(); 

    // If the source directory does not exist, throw an exception. 
    if (!dir.Exists) 
    { 
     throw new DirectoryNotFoundException(
      "Source directory does not exist or could not be found: " 
      + sourceDirName); 
    } 

    // If the destination directory does not exist, create it. 
    if (!Directory.Exists(destDirName)) 
    { 
     Directory.CreateDirectory(destDirName); 
    } 


    // Get the file contents of the directory to copy. 
    FileInfo[] files = dir.GetFiles(); 

    foreach (FileInfo file in files) 
    { 
     // Create the path to the new copy of the file. 
     string temppath = Path.Combine(destDirName, file.Name); 

     // Copy the file. 
     file.CopyTo(temppath, false); 
    } 

    // If copySubDirs is true, copy the subdirectories. 
    if (copySubDirs) 
    { 

     foreach (DirectoryInfo subdir in dirs) 
     { 
      // Create the subdirectory. 
      string temppath = Path.Combine(destDirName, subdir.Name); 

      // Copy the subdirectories. 
      DirectoryCopy(subdir.FullName, temppath, copySubDirs); 
     } 
    } 
}     

任何幫助將不勝感激

回答

2

複製目錄沒有開箱即用的方法。你可以做的最好的是使用擴展方法。看看這個 - http://channel9.msdn.com/Forums/TechOff/257490-How-Copy-directories-in-C/07964d767cc94c3990bb9dfa008a52c8

下面是完整的例子(只是測試和它的作品):

using System; 
using System.IO; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var di = new DirectoryInfo("C:\\SomeFolder"); 
      di.CopyTo("E:\\SomeFolder", true); 
     } 
    } 

public static class DirectoryInfoExtensions 
{ 
    // Copies all files from one directory to another. 
    public static void CopyTo(this DirectoryInfo source, string destDirectory, bool recursive) 
    { 
     if (source == null) 
      throw new ArgumentNullException("source"); 
     if (destDirectory == null) 
      throw new ArgumentNullException("destDirectory"); 

     // If the source doesn't exist, we have to throw an exception. 
     if (!source.Exists) 
      throw new DirectoryNotFoundException("Source directory not found: " + source.FullName); 
     // Compile the target. 
     DirectoryInfo target = new DirectoryInfo(destDirectory); 
     // If the target doesn't exist, we create it. 
     if (!target.Exists) 
      target.Create(); 

     // Get all files and copy them over. 
     foreach (FileInfo file in source.GetFiles()) 
     { 
      file.CopyTo(Path.Combine(target.FullName, file.Name), true); 
     } 

     // Return if no recursive call is required. 
     if (!recursive) 
      return; 

     // Do the same for all sub directories. 
     foreach (DirectoryInfo directory in source.GetDirectories()) 
     { 
      CopyTo(directory, Path.Combine(target.FullName, directory.Name), recursive); 
     } 
    } 
} 

}

1

也許嘗試查看複製之前路徑是否存在。如果它不在那裏,那麼創建它?

string folderPath = Path.GetDirectoryName(path); 
if (!Directory.Exists(folderPath)) 
    Directory.CreateDirectory(folderPath); 
0

明智的做法就像在nithins的答案中一樣。使用您的方法,您可以使用FileInfo.Directory信息來確定文件的來源,然後根據需要在目標中創建此目錄。但nithins鏈接是一個更清潔的解決方案。

相關問題