【问题标题】:Copy directories to another folder将目录复制到另一个文件夹
【发布时间】:2013-05-21 20:01:29
【问题描述】:

我正在尝试将文件和文件夹复制到另一个新文件夹。

我已经将文件复制到这个新文件夹,但是我该如何复制目录。

这是我目前使用的代码。

 DirectoryInfo di = Directory.CreateDirectory(path);

            if(Directory.Exists(mainShape))
            {
                string [] shapeFiles = Directory.GetFiles(mainShape);
                string[] shapeFolders = Directory.GetDirectories(mainShape);

                foreach (string file in shapeFiles)
                {
                    fileName = Path.GetFileName(file);
                    destFile = Path.Combine(path, fileName);
                    System.IO.File.Copy(file, destFile, true);
                }

                foreach (string folder in shapeFolders)
                {

                }

【问题讨论】:

    标签: file model-view-controller copy directory


    【解决方案1】:
    DirectoryInfo src = new DirectoryInfo(@"E:\Test\Dir1");
    DirectoryInfo dest = new DirectoryInfo(@"C:\Dir2");
    CopyDirectory(src, dest);
    
    
    static void CopyDirectory(DirectoryInfo source, DirectoryInfo destination)
        {
            if (!destination.Exists)
            {
                destination.Create();
            }
    
            // Copy all files.
            FileInfo[] files = source.GetFiles();
            foreach (FileInfo file in files)
            {
                file.CopyTo(Path.Combine(destination.FullName,
                    file.Name));
            }
    
            // Process subdirectories.
            DirectoryInfo[] dirs = source.GetDirectories();
            foreach (DirectoryInfo dir in dirs)
            {
                // Get destination directory.
                string destinationDir = Path.Combine(destination.FullName, dir.Name);
    
                // Call CopyDirectory() recursively.
                CopyDirectory(dir, new DirectoryInfo(destinationDir));
            }
        }
    

    http://forums.asp.net/t/1668027.aspx/1

    【讨论】:

      【解决方案2】:
      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)
          {
              var ff = new DirectoryNotFoundException(sourceDirName);
              Console.Write(("Source directory does not exist or could not be found: "  + ff));
              return;
          }
      
          // If the destination directory doesn't exist, create it. 
          if (!Directory.Exists(destDirName))
          {
              Directory.CreateDirectory(destDirName);
          }
              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);
                  }
              }
      }
      

      【讨论】:

        猜你喜欢
        • 2015-10-17
        • 2015-05-05
        • 1970-01-01
        • 1970-01-01
        • 2012-02-15
        • 2017-11-18
        • 1970-01-01
        • 2017-11-16
        相关资源
        最近更新 更多