2011-05-15 8 views

답변

4

방법 디렉토리를 복사하는 방법을 보여줍니다 MSDN's documentation of the DirectoryInfo class에서 가져온 다음 샘플 :

using System; 
using System.IO; 

class CopyDir 
{ 
    public static void CopyAll(DirectoryInfo source, DirectoryInfo target) 
    { 
     if (source.FullName.ToLower() == target.FullName.ToLower()) 
     { 
      return; 
     } 

     // Check if the target directory exists, if not, create it. 
     if (Directory.Exists(target.FullName) == false) 
     { 
      Directory.CreateDirectory(target.FullName); 
     } 

     // Copy each file into it's new directory. 
     foreach (FileInfo fi in source.GetFiles()) 
     { 
      Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name); 
      fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true); 
     } 

     // Copy each subdirectory using recursion. 
     foreach (DirectoryInfo diSourceSubDir in source.GetDirectories()) 
     { 
      DirectoryInfo nextTargetSubDir = 
       target.CreateSubdirectory(diSourceSubDir.Name); 
      CopyAll(diSourceSubDir, nextTargetSubDir); 
     } 
    } 

    public static void Main() 
    { 
     string sourceDirectory = @"c:\sourceDirectory"; 
     string targetDirectory = @"c:\targetDirectory"; 

     DirectoryInfo diSource = new DirectoryInfo(sourceDirectory); 
     DirectoryInfo diTarget = new DirectoryInfo(targetDirectory); 

     CopyAll(diSource, diTarget); 
    } 

    // Output will vary based on the contents of the source directory. 
} 
+0

그들은'string.Compare()'를 사용하지 않았다는 것에 놀랐습니다. – PostMan

관련 문제