Skip to content

Instantly share code, notes, and snippets.

@zafergurel
Last active August 29, 2015 13:56
Show Gist options
  • Save zafergurel/9108657 to your computer and use it in GitHub Desktop.
Save zafergurel/9108657 to your computer and use it in GitHub Desktop.
copyDirectory method that also works for network shares.
/// <summary>
/// Copies one directory to another directory. This works for network share whereas Directory.Move does not...
/// </summary>
/// <param name="sourceDir">source directory</param>
/// <param name="destinationDir">destination directory</param>
/// <param name="deleteSourceDirectory">set to true to delete the source directory</param>
private static void copyDirectory(string sourceDir, string destinationDir, bool deleteSourceDirectory = false)
{
String[] entries;
if (!System.IO.Directory.Exists(sourceDir)) return;
if (!System.IO.Directory.Exists(destinationDir)) System.IO.Directory.CreateDirectory(destinationDir);
// copy sub files
entries = System.IO.Directory.GetFiles(sourceDir);
foreach (string file in entries)
{
System.IO.File.Copy(file, System.IO.Path.Combine(destinationDir, System.IO.Path.GetFileName(file)), true);
}
// copy sub directories
entries = System.IO.Directory.GetDirectories(sourceDir);
foreach (string subDir in entries)
{
copyDirectory(subDir, System.IO.Path.Combine(destinationDir, System.IO.Path.GetFileName(subDir)), deleteSourceDirectory);
}
if (deleteSourceDirectory) System.IO.Directory.Delete(sourceDir, true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment