Skip to content

Instantly share code, notes, and snippets.

@kyrathasoft
Created October 2, 2019 23:16
Show Gist options
  • Select an option

  • Save kyrathasoft/70d2bd4bbd955cadbf5380bc6e0d77e0 to your computer and use it in GitHub Desktop.

Select an option

Save kyrathasoft/70d2bd4bbd955cadbf5380bc6e0d77e0 to your computer and use it in GitHub Desktop.
A method that faithfully copies an entire directory structure to another directory
public static bool CopyTo(string targetDirectory, string destinationDirectory)
{
bool blnSuccess = false;
try{
if(Directory.Exists(targetDirectory)){
foreach (string dirPath in Directory.GetDirectories(targetDirectory, "*",
SearchOption.AllDirectories)){
Directory.CreateDirectory(dirPath.Replace(targetDirectory, destinationDirectory));
}
//Copy all the files & Replaces any files with the same name
foreach (string newPath in Directory.GetFiles(targetDirectory, "*.*",
SearchOption.AllDirectories)){
File.Copy(newPath, newPath.Replace(targetDirectory, destinationDirectory), true);
blnSuccess = true;
}
}
}catch(Exception exCopyTo){
Console.WriteLine("Error in ioLibrary: " + exCopyTo.Message);
}
return blnSuccess;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment