2011-08-22 195 views
41

如何将一个目录中的所有内容复制到另一个目录中,并将每个文件循环出去?复制目录中的所有文件

+0

看吧:http://stackoverflow.com/questions/206323/how-to-execute-command-line-in-c-get-std-out-results并在那里住了命令“副本\ *。* YOURDIR“ – fritzone

回答

1

你不行。但你可以使用某种简洁的代码,如Directory.GetFiles(mydir).ToList().ForEach(f => File.Copy(f, otherdir + "\\" f);

+3

这不起作用。 –

78

你不能。 DirectoryDirectoryInfo均未提供Copy方法。你需要自己实现这个。

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

请阅读注意事项以了解此简单方法的一些问题。

+0

这只会复制文件,它不会创建任何子目录。因此,如果目的地尚未具有相同的目录结构,则需要在开始时添加一行以创建目的地(如果目的地不存在)。 – Ross

+1

@Ross:的确,固定的。请注意,这不会复制访问权限等安全属性。 –

+1

检查文件和文件夹是否存在,它工作的很好。如果(!Directory.Exists(targetDir))... if(!File.Exists(file)) – Misi

0

执行xcopy source_directory\*.* destination_directory作为外部命令。当然这只会在Windows机器上运行。

+0

如果不需要,你不应该使用系统调用。在这里,这绝对没有必要。 –

+0

有必要给出这个问题陈述**与循环每个文件**。 – m0skit0

+0

你认为xcopy在做什么?他只是不想循环,因为他认为有一个更简单的方法。没有。一般而言,系统调用既不容易,也不是一个好方法。强烈不鼓励! –

8

您可以使用VB的FileSystem.CopyDirectory方法来简化任务:

using Microsoft.VisualBasic.FileIO; 

foo(){ 
    FileSystem.CopyDirectory(directoryPath, tempPath); 
} 
+2

我想念VB。人们说你可以在C#中完成所有相同的事情。 –

-1
Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + @"resources\html") 
    .ToList() 
    .ForEach(f => File.Copy(f, folder + "\\" + f.Substring(f.LastIndexOf("\\")))); 
2
using System.IO; 

string sourcePath = @"D:\test"; 
string targetPath = @"D:\test_new"; 
if (!Directory.Exists(targetPath)) 
{ 
    Directory.CreateDirectory(targetPath); 
} 
foreach (var srcPath in Directory.GetFiles(sourcePath)) 
{ 
    //Copy the file from sourcepath and place into mentioned target path, 
    //Overwrite the file if same file is exist in target path 
    File.Copy(srcPath, srcPath.Replace(sourcePath, targetPath), true); 
} 
0

这个伟大的工程!它将复制子目录,或者您可以将所有子目录中的所有文件转储到一个位置。

/// AUTHOR : Norm Petroff 
/// <summary> 
/// Takes the files from the PathFrom and copies them to the PathTo. 
/// </summary> 
/// <param name="pathFrom"></param> 
/// <param name="pathTo"></param> 
/// <param name="filesOnly">Copies all files from each directory to the "PathTo" and removes directory.</param> 
static void CopyFiles(String pathFrom, String pathTo, Boolean filesOnly) 
{ 
    foreach(String file in Directory.GetFiles(pathFrom)) 
    { 
     // Copy the current file to the new path. 
     File.Copy(file, Path.Combine(pathTo, Path.GetFileName(file)), true); 

     // Get all the directories in the current path. 
     foreach (String directory in Directory.GetDirectories(pathFrom)) 
     { 
      // If files only is true then recursively get all the files. They will be all put in the original "PathTo" location 
      // without the directories they were in. 
      if (filesOnly) 
      { 
       // Get the files from the current directory in the loop. 
       CopyFiles(directory, pathTo, filesOnly); 
      } 
      else 
      { 
       // Create a new path for the current directory in the new location.      
       var newDirectory = Path.Combine(pathTo, new DirectoryInfo(directory).Name); 

       // Copy the directory over to the new path location if it does not already exist. 
       if (!Directory.Exists(newDirectory)) 
       { 
        Directory.CreateDirectory(newDirectory); 
       } 

       // Call this routine again with the new path. 
       CopyFiles(directory, newDirectory, filesOnly); 
      } 
     } 
    } 
} 
相关问题