2013-09-25 35 views
0

我現在很困擾這個問題。我需要複製(更新)從Folder1 \ directory1到Updates \ directory1的所有內容,覆蓋相同的文件,但不會刪除已存在於Updated \ directory1但Folder1 \ directory1上不存在的文件。爲了使我的問題更清楚,這是我的預計業績:將文件和子目錄複製到包含現有文件的另一個目錄

C:\ Folder1中\ directory1中

subfolder1

subtext1.txt(2KB)

subfolder2

name.txt(2KB)

C:\更新\ directory1中

subfolder1

subtext1.txt(1KB)

subtext2.txt(2KB)

預期結果:

C:\更新\ directory1中

subfolder1

subtext1.txt(2KB)< ---更新

subtext2.txt(2KB)

子文件夾2 < ---增加了

name.txt(2KB)< ---附加

我目前使用Directory.Move(source, destination),但我有大約因爲它的一些目標部分麻煩的目標文件夾是非存在。我唯一的想法是使用String.Trim來確定是否有其他文件夾,但我不能真正使用它,因爲目錄應該是動態的(可以有更多的子目錄或更多的文件夾)。我很困難。你能推薦一些提示或一些代碼讓我的東西移動嗎?謝謝!

回答

0
// This can be handled any way you want, I prefer constants 
const string STABLE_FOLDER = @"C:\temp\stable\"; 
const string UPDATE_FOLDER = @"C:\temp\updated\"; 

// Get our files (recursive and any of them, based on the 2nd param of the Directory.GetFiles() method 
string[] originalFiles = Directory.GetFiles(STABLE_FOLDER,"*", SearchOption.AllDirectories); 

// Dealing with a string array, so let's use the actionable Array.ForEach() with a anonymous method 
Array.ForEach(originalFiles, (originalFileLocation) => 
{  
    // Get the FileInfo for both of our files 
    FileInfo originalFile = new FileInfo(originalFileLocation); 
    FileInfo destFile = new FileInfo(originalFileLocation.Replace(STABLE_FOLDER, UPDATE_FOLDER)); 
            // ^^ We can fill the FileInfo() constructor with files that don't exist... 

    // ... because we check it here 
    if (destFile.Exists) 
    { 
     // Logic for files that exist applied here; if the original is larger, replace the updated files... 
     if (originalFile.Length > destFile.Length) 
     { 
      originalFile.CopyTo(destFile.FullName, true); 
     } 
    }  
    else // ... otherwise create any missing directories and copy the folder over 
    { 
     Directory.CreateDirectory(destFile.DirectoryName); // Does nothing on directories that already exist 
     originalFile.CopyTo(destFile.FullName,false); // Copy but don't over-write 
    } 

}); 

這是一個快速的一次性......沒有錯誤處理在這裏實現。

+0

這正是我所需要的。現貨和非常翔實的評論。非常感謝@Steiner:D – user2595220

2

我從MSDN http://msdn.microsoft.com/en-us/library/cc148994.aspx這個例子,我想這是你在尋找什麼

// To copy all the files in one directory to another directory. 
    // Get the files in the source folder. (To recursively iterate through 
    // all subfolders under the current directory, see 
    // "How to: Iterate Through a Directory Tree.") 
    // Note: Check for target path was performed previously 
    //  in this code example. 
    if (System.IO.Directory.Exists(sourcePath)) 
    { 
     string[] files = System.IO.Directory.GetFiles(sourcePath); 

     // Copy the files and overwrite destination files if they already exist. 
     foreach (string s in files) 
     { 
      // Use static Path methods to extract only the file name from the path. 
      fileName = System.IO.Path.GetFileName(s); 
      destFile = System.IO.Path.Combine(targetPath, fileName); 
      System.IO.File.Copy(s, destFile, true); 
     } 
    } 
    else 
    { 
     Console.WriteLine("Source path does not exist!"); 
    } 

如果你需要處理不存在的文件夾路徑,你應該創建一個新的文件夾

if (System.IO.Directory.Exists(targetPath){ 
    System.IO.Directory.CreateDirectory(targetPath); 
} 
0

這將幫助你它是一個通用的遞歸函數,所以總是合併子文件夾。

/// <summary> 
    /// Directories the copy. 
    /// </summary> 
    /// <param name="sourceDirPath">The source dir path.</param> 
    /// <param name="destDirName">Name of the destination dir.</param> 
    /// <param name="isCopySubDirs">if set to <c>true</c> [is copy sub directories].</param> 
    /// <returns></returns> 
    public static void DirectoryCopy(string sourceDirPath, string destDirName, bool isCopySubDirs) 
    { 
     // Get the subdirectories for the specified directory. 
     DirectoryInfo directoryInfo = new DirectoryInfo(sourceDirPath); 
     DirectoryInfo[] directories = directoryInfo.GetDirectories(); 
     if (!directoryInfo.Exists) 
     { 
      throw new DirectoryNotFoundException("Source directory does not exist or could not be found: " 
       + sourceDirPath); 
     } 
     DirectoryInfo parentDirectory = Directory.GetParent(directoryInfo.FullName); 
     destDirName = System.IO.Path.Combine(parentDirectory.FullName, destDirName); 

     // 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 = directoryInfo.GetFiles(); 

     foreach (FileInfo file in files) 
     { 
      string tempPath = System.IO.Path.Combine(destDirName, file.Name); 

      if (File.Exists(tempPath)) 
      { 
       File.Delete(tempPath); 
      } 

      file.CopyTo(tempPath, false); 
     } 
     // If copying subdirectories, copy them and their contents to new location using recursive function. 
     if (isCopySubDirs) 
     { 
      foreach (DirectoryInfo item in directories) 
      { 
       string tempPath = System.IO.Path.Combine(destDirName, item.Name); 
       DirectoryCopy(item.FullName, tempPath, isCopySubDirs); 
      } 
     } 
    } 
相關問題