当前位置 : 主页 > 编程语言 > c语言 >

深层目录文件复制,C# 递归,录音录像图片文件过多,用于测试程序

来源:互联网 收集:自由互联 发布时间:2021-06-25
1 /// summary 2 /// 录音录像图片文件过多只复制目录的前几个文件,用于测试程序 3 /// d:\file/images/2019-10/01/01/xxxxx.jpg(前几个文件) 4 /// 复制到 5 /// E:\file/images/2019-10/01/01/xxxxx.jpg 6 /// 7

 

 

 1 /// <summary>
 2         /// 录音录像图片文件过多只复制目录的前几个文件,用于测试程序
 3         /// d:\file/images/2019-10/01/01/xxxxx.jpg(前几个文件)
 4         /// 复制到
 5         /// E:\file/images/2019-10/01/01/xxxxx.jpg
 6         /// 
 7         /// copyfiles("d:\file","e:\file");
 8         /// </summary>
 9         /// <param name="path">源目录</param>
10         /// <param name="toPath">目的目录</param>
11         /// <param name="Num">文件个数</param>
12         public static void CopyFiles(string path,string toPath,int Num)
13         {
14             var logger = NLog.LogManager.GetCurrentClassLogger();
15             DirectoryInfo dir = new DirectoryInfo(path);
16             DirectoryInfo[] childs = dir.GetDirectories();
17             string newdirStr;
18             if (dir.Name.IndexOf(:) == -1)
19             {
20                 DirectoryInfo newdir = Directory.CreateDirectory(toPath + "\\" + dir.Name);
21                 newdirStr = newdir.FullName;
22             }
23             else
24             {
25                 newdirStr = toPath;
26             }
27             if (childs.Length>0)
28             {
29                 foreach (var child in childs)
30                 {
31                     if ((child.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
32                     {
33                         continue;
34                     }
35                     CopyFiles(child.FullName, newdirStr, Num);
36                 }
37             }
38             else
39             {
40                 dir.GetFiles().Take(Num).ToList().ForEach(f => {
41                     string newfilename = newdirStr + "\\" + f.Name;
42                     File.Copy(f.FullName, newfilename, true);
43                     logger.Info($"复制文件:{f.FullName}");
44                     logger.Info($"复制文件:{newfilename}");
45                 });
46             }
47         }
网友评论