2017-05-27 98 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

相关问题