2011-02-04 14 views

答えて

2

ディレクトリをコピーするための方法はありません。あなたができることは、拡張メソッドを使うことです。これを見て持っている - ここでhttp://channel9.msdn.com/Forums/TechOff/257490-How-Copy-directories-in-C/07964d767cc94c3990bb9dfa008a52c8

は完全な例である(ただ、テストされ、それが動作します):

using System; 
using System.IO; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var di = new DirectoryInfo("C:\\SomeFolder"); 
      di.CopyTo("E:\\SomeFolder", true); 
     } 
    } 

public static class DirectoryInfoExtensions 
{ 
    // Copies all files from one directory to another. 
    public static void CopyTo(this DirectoryInfo source, string destDirectory, bool recursive) 
    { 
     if (source == null) 
      throw new ArgumentNullException("source"); 
     if (destDirectory == null) 
      throw new ArgumentNullException("destDirectory"); 

     // If the source doesn't exist, we have to throw an exception. 
     if (!source.Exists) 
      throw new DirectoryNotFoundException("Source directory not found: " + source.FullName); 
     // Compile the target. 
     DirectoryInfo target = new DirectoryInfo(destDirectory); 
     // If the target doesn't exist, we create it. 
     if (!target.Exists) 
      target.Create(); 

     // Get all files and copy them over. 
     foreach (FileInfo file in source.GetFiles()) 
     { 
      file.CopyTo(Path.Combine(target.FullName, file.Name), true); 
     } 

     // Return if no recursive call is required. 
     if (!recursive) 
      return; 

     // Do the same for all sub directories. 
     foreach (DirectoryInfo directory in source.GetDirectories()) 
     { 
      CopyTo(directory, Path.Combine(target.FullName, directory.Name), recursive); 
     } 
    } 
} 

}

+0

このコードを使用してエラーが発生すると、system.IO.directoryinfoにcopytoの定義が含まれていません – bobthemac

+0

この[MSDN記事](http://msdn.microsoft.com/en-us/library/bb383977)を参照してください。 aspx)の拡張メソッドの使い方について説明します。 – Mayank

1

を理解されるであろう、これまで

{ 
    string SelectedPath = (string)e.Argument; 
    string sourceDirName; 
    string destDirName; 
    bool copySubDirs; 
    DirectoryCopy(".", SelectedPath, true); 

    DirectoryInfo dir = new DirectoryInfo(sourceDirName); 
    DirectoryInfo[] dirs = dir.GetDirectories(); 

    // If the source directory does not exist, throw an exception. 
    if (!dir.Exists) 
    { 
     throw new DirectoryNotFoundException(
      "Source directory does not exist or could not be found: " 
      + sourceDirName); 
    } 

    // If the destination directory does not exist, create it. 
    if (!Directory.Exists(destDirName)) 
    { 
     Directory.CreateDirectory(destDirName); 
    } 


    // Get the file contents of the directory to copy. 
    FileInfo[] files = dir.GetFiles(); 

    foreach (FileInfo file in files) 
    { 
     // Create the path to the new copy of the file. 
     string temppath = Path.Combine(destDirName, file.Name); 

     // Copy the file. 
     file.CopyTo(temppath, false); 
    } 

    // If copySubDirs is true, copy the subdirectories. 
    if (copySubDirs) 
    { 

     foreach (DirectoryInfo subdir in dirs) 
     { 
      // Create the subdirectory. 
      string temppath = Path.Combine(destDirName, subdir.Name); 

      // Copy the subdirectories. 
      DirectoryCopy(subdir.FullName, temppath, copySubDirs); 
     } 
    } 
}     

であるあなたはhttp://msdn.microsoft.com/en-us/library/bb762914.aspxを見て撮影したことがありますか?

+0

葉私はそれを試したように見えたが、それは私には動作しませんでした何らかの理由でコード – bobthemac

+1

ええ、理由は何ですか?それは誤りを与えなければならない。 – Karl

+0

なぜ私はエラーのすべてのコードを与えるだけで私にエラーを与えるか分からない。 – bobthemac

1

コピーの前にパスが存在するかどうかを確認してください。それがなければ、それを作成しますか?

string folderPath = Path.GetDirectoryName(path); 
if (!Directory.Exists(folderPath)) 
    Directory.CreateDirectory(folderPath); 
0

これを行うスマートな方法は、nithins答えのようです。あなたのアプローチを使用すると、FileInfo.Directory情報を使用してファイルのソースを特定し、必要に応じて宛先にこのディレクトリを作成することができます。しかし、nithinsリンクはよりクリーンなソリューションです。

関連する問題

 関連する問題