public static void Copy(string sourceDirName, string destDirName, bool copySubDirs = true, bool overwriteFiles = false)
{
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
DirectoryInfo[] dirs = dir.GetDirectories();
// Source directory must exist!
if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ sourceDirName);
}
// Create the dir if it doesnt exist
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, overwriteFiles);
}
// Do we recursivly copy?
if (copySubDirs)
{
foreach (DirectoryInfo subdir in dirs)
{
string temppath = Path.Combine(destDirName, subdir.Name);
Copy(subdir.FullName, temppath, copySubDirs, overwriteFiles);
}
}
}