2011-08-22 17 views

答えて

1

できません。しかし、あなたのような簡潔なコードを使用することができますDirectory.GetFiles(mydir).ToList().ForEach(f => File.Copy(f, otherdir + "\\" f);

+3

これは再帰的には機能しません。 –

78

することはできません。 DirectoryでもDirectoryInfoも、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

@ロス:確かに、修正されました。これはアクセス権のようなセキュリティ属性をコピーしないことに注意してください。 –

+1

ファイルとフォルダが存在するかどうかを確認するとうまくいきます。 if(!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

これは素晴らしい作品!サブディレクトリをコピーするか、すべてのサブディレクトリのすべてのファイルを1つの場所にダンプするだけです。

/// 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); 
      } 
     } 
    } 
} 
関連する問題