2014-02-06 58 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

相关问题