在C#中递归复制文件夹,是否仍然存在?

在C#中递归复制文件夹,是否仍然存在?,c#,.net,file,file-io,rename,C#,.net,File,File Io,Rename,我查看System.IO.File.Move,将文件“移动”到一个新名称 System.IO.File.Move("oldfilename", "newfilename"); 但是我用这种方法是不够的 事实上,我希望递归地将文件夹及其所有子文件夹复制到一个新路径中,并更改这些文件的名称。谁能给我一段代码 如果要移动文件夹: string newDirPath = "E:\\New\\destDirName"; Directory.CreateDirectory(newDirPath); Dir

我查看
System.IO.File.Move
,将文件“移动”到一个新名称

System.IO.File.Move("oldfilename", "newfilename");
但是我用这种方法是不够的


事实上,我希望递归地将文件夹及其所有子文件夹复制到一个新路径中,并更改这些文件的名称。谁能给我一段代码

如果要移动文件夹:

string newDirPath = "E:\\New\\destDirName";
Directory.CreateDirectory(newDirPath);
Directory.Move("D:\\sourceDirPath", newDirPath);
如果您想:


为了更安全和递归地循环每个子目录,相关主题:这非常相似,但不会更改文件的名称:源:使用此命令,我们可以访问所有文件夹和子文件夹“foreach(directory.getdirpath中的string dirPath(SourcePath,“*”,SearchOption.AllDirectories))”ok?@MathLover是的。
//Create all of the directories
foreach (string dirPath in Directory.GetDirectories(SourcePath, "*", 
    SearchOption.AllDirectories))
    Directory.CreateDirectory(dirPath.Replace(SourcePath, DestinationPath));

//Copy all the files & Replaces any files with the same name
foreach (string newPath in Directory.GetFiles(SourcePath, "*.*", 
    SearchOption.AllDirectories))
    File.Copy(newPath, newPath.Replace(SourcePath, DestinationPath), true);