2017-05-27 39 views
-1

我想將文件夾中的所有內容複製到兩個文件目標文件夾中。將文件夾中的所有內容複製到兩個文件目標文件夾中

foreach (string newPath in Directory.GetFiles(@"E:\autotransfer", "*.*", 
      SearchOption.AllDirectories)) 
      File.Copy(newPath, newPath.Replace(@"E:\autotransfer", 
    @"E:\autotransferbackup"), true); 

    foreach (string newPath in Directory.GetFiles(@"E:\autotransfer", "*.*", 
      SearchOption.AllDirectories)) 
      File.Copy(newPath, newPath.Replace(@"E:\autotransfer", 
    @"E:\autotransferbackupcp"), true); 
+4

請更具體一些。除了使用'string.Replace()'來操作文件路徑不是一個好主意,你發佈的一小段代碼看起來好像會起作用。或者至少做_something_。你說它「不起作用」_。在某種程度上,_specifically_,代碼不起作用?提供一個可靠地再現問題的良好[mcve],並解釋_precisely_該代碼的功能,以及您希望它執行的操作。 (請注意'GetFiles()'返回的路徑的外殼可能與您的'Replace()'調用中的外殼不匹配。) –

回答

0

您可以使用此代碼,更多信息請參考這裏的答案:Copy all files in directory

void Copy(string sourceDir, string targetDir) 
    { 
     Directory.CreateDirectory(targetDir); 

     foreach (var file in Directory.GetFiles(sourceDir)) 
      File.Copy(file, Path.Combine(targetDir, Path.GetFileName(file))); 

     foreach (var directory in Directory.GetDirectories(sourceDir)) 
      Copy(directory, Path.Combine(targetDir, Path.GetFileName(directory))); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     Copy("E:\autotransfer", "E:\autotransferbackup"); 
     Copy("E:\autotransfer", "E:\autotransferbackupcp"); 
    } 

如果目錄結構是不一樣的,那麼你就需要檢查該文件夾是否存在,如果不是,先創建它,然後複製這些文件。

+0

謝謝sir.It工作。我的下一個問題是每次都會如何自動傳輸該文件夾autotransfer有一個內容,它會自動發送到兩個目錄。因爲我是編程的初學者,我需要一些幫助。謝謝你,先生。 – Sam

+0

如果我只想複製特定目錄中的兩個子文件夾的內容,該怎麼做。 – Sam

0

從這裏拍攝於MSDN:https://msdn.microsoft.com/en-us/library/bb762914(v=vs.110).aspx

您可以複製此功能,並在代碼中使用它。

希望這會有所幫助。

using System; 
using System.IO; 

class DirectoryCopyExample 
{ 
    static void Main() 
    { 
     // Copy from the current directory, include subdirectories. 
     DirectoryCopy(".", @".\temp", true); 
    } 

    private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs) 
    { 
     // Get the subdirectories for the specified directory. 
     DirectoryInfo dir = new DirectoryInfo(sourceDirName); 

     if (!dir.Exists) 
     { 
      throw new DirectoryNotFoundException(
       "Source directory does not exist or could not be found: " 
       + sourceDirName); 
     } 

     DirectoryInfo[] dirs = dir.GetDirectories(); 
     // If the destination directory doesn't exist, create it. 
     if (!Directory.Exists(destDirName)) 
     { 
      Directory.CreateDirectory(destDirName); 
     } 

     // Get the files in the directory and copy them to the new location. 
     FileInfo[] files = dir.GetFiles(); 
     foreach (FileInfo file in files) 
     { 
      string temppath = Path.Combine(destDirName, file.Name); 
      file.CopyTo(temppath, false); 
     } 

     // If copying subdirectories, copy them and their contents to new location. 
     if (copySubDirs) 
     { 
      foreach (DirectoryInfo subdir in dirs) 
      { 
       string temppath = Path.Combine(destDirName, subdir.Name); 
       DirectoryCopy(subdir.FullName, temppath, copySubDirs); 
      } 
     } 
    } 
} 
+0

謝謝你的迴應。我首先嚐試了上面的第一條建議/答案,它適用於我。也許在其他情況下,我會嘗試你的代碼先生。謝謝你,先生。 – Sam

相關問題