Created
September 7, 2012 05:05
-
-
Save fresky/3663334 to your computer and use it in GitHub Desktop.
Copy directory in C#
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
private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs) | |
{ | |
DirectoryInfo dir = new DirectoryInfo(sourceDirName); | |
DirectoryInfo[] dirs = dir.GetDirectories(); | |
if (!dir.Exists) | |
{ | |
throw new DirectoryNotFoundException( | |
"Source directory does not exist or could not be found: " | |
+ sourceDirName); | |
} | |
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, false); | |
} | |
if (copySubDirs) | |
{ | |
foreach (DirectoryInfo subdir in dirs) | |
{ | |
string temppath = Path.Combine(destDirName, subdir.Name); | |
directoryCopy(subdir.FullName, temppath, copySubDirs); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment