2014-02-06 67 views
0

De將文件夾數據複製到網絡中的其他文件夾

我必須將文件夾和文件從一個網絡文件夾複製到其他文件夾。有些文件因特殊字符命名而無法複製。 有這麼多的文件夾和子文件夾與3 GB的數據。爲了避免這個問題,我想編寫一個C#程序,它可以將所有文件夾,子文件夾和文件 與日誌文件(記事本)複製。日誌文件應該注意非複製文件的細節和路徑,以便於進一步追蹤它們。任何人都可以通過提供一個c#程序或者至少一個參考來快速幫助我 。控制檯或Windows窗體應用程序,我使用Visual Studio 2010和Windows 7

複製像下面

複製形式: - https://ap.sharepoint.a5-group.com/cm/Shared文件/ IRD/EA

要: - https://cr.sp.a5-group.com/sites/cm/Shared文件/ IRD/EA

+0

你有沒有嘗試任何代碼?看起來你會想從源代碼到目的地執行遞歸文件複製。 –

回答

1

這裏,試試這個:

編輯:我的回答適用於本地網絡目錄,我不提,你想從HTTPS複製directiories,爲您必須使用Web客戶端與Credenti ALS

class DirectoryCopyExample 
    { 
     string pathFrom = "C:\\someFolder"; 
     string pathTo = "D:\\otherFolder"; 
     string LogText = string.Empty; 
     static void Main() 
     { 
      // Copy from the current directory, include subdirectories. 
      DirectoryCopy(pathFrom, pathTo, true); 
     } 

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

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

      // 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) 
      { 
       try 
       { 
        string temppath = Path.Combine(destDirName, file.Name); 
        file.CopyTo(temppath, false); 
       } 
       catch(Exception) 
       { 
        //Write Files to Log whicht couldn't be copy 
        LogText += DateTime.Now.ToString() + ": " + file.FullName; 
       } 
      } 

      // 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); 
       } 
      } 
     } 
    } 

你必須變量LogTex保存到文件末尾,或任何你需要

來源:http://msdn.microsoft.com/en-us/library/bb762914(v=vs.110).aspx

+0

非常感謝Magix,這非常有幫助 – Ajain

相關問題