private static bool CopyDirectory(string sourceDirName, string destDirName)
{
bool success = true;
var sourceDirectory = new DirectoryInfo(sourceDirName);
if (!sourceDirectory.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ sourceDirName);
}
if (!Directory.Exists(destDirName))
{
Directory.CreateDirectory(destDirName);
}
foreach (FileInfo file in sourceDirectory.GetFiles())
{
var destFileName = Path.Combine(destDirName, file.Name);
try
{
file.CopyTo(destFileName, true);
}
catch (Exception ex)
{
if (!(ex is IOException || ex is UnauthorizedAccessException || ex is SecurityException))
throw;
// Maybe we don't need to write it...it hasn't changed since a previous download?
if (!SameFileContent(destFileName, file.FullName))
success = false;
}
}
foreach (DirectoryInfo subdir in sourceDirectory.GetDirectories())
{
success = CopyDirectory(subdir.FullName, Path.Combine(destDirName, subdir.Name)) && success;
}
return success;
}