You probably are having problems with the path. If you have a slash at the end, then you have a problem. The following seems to work:
private static void CopyDirectory(string sourcePath, string destPath)
{
if (!Directory.Exists(destPath))
{
Directory.CreateDirectory(destPath);
}
var fixedSource = new DirectoryInfo(sourcePath);
var fixedDest = new DirectoryInfo(destPath);
foreach (var file in fixedSource.GetFiles())
{
string dest = Path.Combine(fixedDest.FullName, file.Name);
file.CopyTo(dest);
}
foreach (var folder in fixedSource.GetDirectories())
{
string dest = Path.Combine(destPath, folder.Name);
CopyDirectory(folder.FullName, dest);
}
}
This is not the way I would recommend that you do it. I would have a non-recursive method that would fix any path issues, then your code will work, but you may have an issue with duplicates, and you need to deal with that also.