-
-
Save StickNitro/60307ce10041f8e392bc7efe4ebc279a to your computer and use it in GitHub Desktop.
Copy Directory helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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