Skip to content

Instantly share code, notes, and snippets.

@StickNitro
Forked from RichiCoder1/Utilities.cake
Created March 14, 2017 11:08
Show Gist options
  • Save StickNitro/60307ce10041f8e392bc7efe4ebc279a to your computer and use it in GitHub Desktop.
Save StickNitro/60307ce10041f8e392bc7efe4ebc279a to your computer and use it in GitHub Desktop.
Copy Directory helper
public void CopyDirectory(string source, string destination)
{
// Get the subdirectories for the specified directory.
var dir = new DirectoryInfo(source);
var dirs = dir.GetDirectories();
if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ source);
}
// If the destination directory doesn't exist, create it.
if (!Directory.Exists(destination))
{
Directory.CreateDirectory(destination);
}
// Get the files in the directory and copy them to the new location.
var files = dir.GetFiles();
foreach (var file in files)
{
var temppath = System.IO.Path.Combine(destination, file.Name);
file.CopyTo(temppath, false);
}
// Copy all of the subdirectories
foreach (var subdir in dirs)
{
var temppath = System.IO.Path.Combine(destination, subdir.Name);
CopyDirectory(subdir.FullName, temppath);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment