加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 01:25:46 所属栏目:百科 来源:网络整理
导读:? ? 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"

?

?

 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         }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读