复制文件夹

前阶段遇见复制文件夹问题,一时想到要用递归,总结如下:

复制文件夹
 1 static void Main(string[] args)
 2         {
 3             CopyDirectroy(@"c:\Dell", @"d:\Dell");
 4         }
 5         static void CopyDirectroy(string sourePath, string targetPath)
 6         {
 7             DirectoryInfo sourceDir = new DirectoryInfo(sourePath);//
 8             DirectoryInfo targetDir = new DirectoryInfo(targetPath);//
 9             if (!sourceDir.Exists)
10             {
11                 return;
12             }
13             if (!targetDir.Exists)
14             {
15                 Directory.CreateDirectory(targetPath);//
16             }
17             FileInfo[] files = sourceDir.GetFiles();//
18             int filesLength = files.Length;//
19             for (int i = 0; i < filesLength; i++)//
20             {
21                 File.Copy(files[i].FullName, targetPath + "\\" + files[i].Name, true);//
22             }
23             DirectoryInfo[] dirs = sourceDir.GetDirectories();//
24             for (int i = 0; i < dirs.Length; i++)
25             {
26                 CopyDirectroy(dirs[i].FullName, targetPath + "\\" + dirs[i].Name);//
27             }
28         }
原文地址:https://www.cnblogs.com/hfliyi/p/2624360.html