无论如何,在C#中递归复制文件夹?

时间:2016-02-24 15:36:58

标签: c# .net file file-io rename

我查看System.IO.File.Move,将文件“移动”为新名称。

System.IO.File.Move("oldfilename", "newfilename");

但这对我来说还不够用这种方法。

实际上,我希望以递归方式将文件夹及其所有子文件夹复制到新路径中,并更改这些文件的名称。有人可以带我一些代码吗?

1 个答案:

答案 0 :(得分:1)

如果要移动文件夹:

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

如果你想要copy a folder

//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);