C# 文件夹拷贝

时间:2021-06-02 21:36:19
 1 /// <summary>
 2         /// 整个文件夹拷贝
 3         /// </summary>
 4         /// <param name="yanfilepath"></param>
 5         /// <param name="mudifilepath">目标</param>
 6         public void CopyFilesDirs(string yanfilepath, string mudifilepath)
 7         {
 8             try
 9             {
10                 if (!Directory.Exists(mudifilepath))
11                 {
12                     Directory.CreateDirectory(mudifilepath);
13                 }
14                 string[] arrDirs = Directory.GetDirectories(yanfilepath);
15                 string[] arrFiles = Directory.GetFiles(yanfilepath);
16                 if (arrFiles.Length != 0)
17                 {
18                     for (int i = 0; i < arrFiles.Length; i++)
19                         File.Copy(yanfilepath + "\\" + Path.GetFileName(arrFiles[i]), mudifilepath + "\\"
20                         + Path.GetFileName(arrFiles[i]), true);
21                 }
22                 if (arrDirs.Length != 0)
23                 {
24                     for (int i = 0; i < arrDirs.Length; i++)
25                     {
26                         Directory.CreateDirectory(mudifilepath + "\\" + Path.GetFileName(arrDirs[i]));
27                         //递归调用   
28                         CopyFilesDirs(yanfilepath + "\\" + Path.GetFileName(arrDirs[i]),
29                         mudifilepath + "\\" + Path.GetFileName(arrDirs[i]));
30                     }
31                 }
32                 else
33                     return;
34             }
35             catch
36             {
37 
38             }
39         }