/// <summary>
/// 录音录像图片文件过多只复制目录的前几个文件,用于测试程序
/// d:\file/images/2019-10/01/01/xxxxx.jpg(前几个文件)
/// 复制到
/// E:\file/images/2019-10/01/01/xxxxx.jpg
///
/// copyfiles("d:\file","e:\file");
/// </summary>
/// <param name="path">源目录</param>
/// <param name="toPath">目的目录</param>
/// <param name="Num">文件个数</param>
public static void CopyFiles(string path,string toPath,int Num)
{
var logger = NLog.LogManager.GetCurrentClassLogger();
DirectoryInfo dir = new DirectoryInfo(path);
DirectoryInfo[] childs = dir.GetDirectories();
string newdirStr;
if (dir.Name.IndexOf(':') == -)
{
DirectoryInfo newdir = Directory.CreateDirectory(toPath + "\\" + dir.Name);
newdirStr = newdir.FullName;
}
else
{
newdirStr = toPath;
}
if (childs.Length>)
{
foreach (var child in childs)
{
if ((child.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
{
continue;
}
CopyFiles(child.FullName, newdirStr, Num);
}
}
else
{
dir.GetFiles().Take(Num).ToList().ForEach(f => {
string newfilename = newdirStr + "\\" + f.Name;
File.Copy(f.FullName, newfilename, true);
logger.Info($"复制文件:{f.FullName}");
logger.Info($"复制文件:{newfilename}");
});
}
}