I want to copy the specific directory and all sub directories and files with C#.
With this code I have a problem. This code creates another folder in a sub folder with the same name, but I can't find where the mistake is.
Directory path => D:\FolderAAA\FolderBBB\File1
Target (result): => E:\FolderAAA\FolderBBB\FolderBBB\File1
(issue with double bbb folder)
This is my code
private void CopyTheDirectory(string directoryPath, string targetPath)
{
DirectoryInfo d_info = new DirectoryInfo(directoryPath);
Directory.CreateDirectory(Path.Combine(targetPath, d_info.Name));
var files = Directory.GetFiles(d_info.FullName);
var directories = Directory.GetDirectories(directoryPath);
foreach(var file in files)
{
File.Copy(file, Path.Combine(Path.Combine(targetPath, d_info.Name), Path.GetFileName(file)));
}
foreach(var directory in directories)
{
CopyTheDirectory(directory, Path.Combine(Path.Combine(targetPath, d_info.Name), Path.GetFileName(directory)));
}
}