Sunday, July 21, 2013

Copy directory in C#

The following method copy a directory and its contents to another location:


private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs=true)
 {
 DirectoryInfo dir = new DirectoryInfo(sourceDirName);
 DirectoryInfo[] dirs = dir.GetDirectories();

 if (!dir.Exists)
 {
  throw new DirectoryNotFoundException(
   "Source directory does not exist or could not be found: "
   + sourceDirName);
 }

 if (!Directory.Exists(destDirName))
 {
  Directory.CreateDirectory(destDirName);
 }

 FileInfo[] files = dir.GetFiles();
 foreach (FileInfo file in files)
 {
  string temppath = Path.Combine(destDirName, file.Name);
  file.CopyTo(temppath, false);
 }

 if (copySubDirs)
 {
  foreach (DirectoryInfo subdir in dirs)
  {
   string temppath = Path.Combine(destDirName, subdir.Name);
   DirectoryCopy(subdir.FullName, temppath, copySubDirs);
  }
 }
}

No comments:

Post a Comment