public void TraverseFolder(DirectoryInfo dir, Action <FileInfo, List <Cpk>, string> act, List <Cpk> cpks, string zipName) //搜索文件夹中的文件 { foreach (var file in dir.GetFiles()) { try { act(file, cpks, zipName); if (cpks.Count() > 4500) { CpkRepository.BulkInsertAll(cpks); cpks.Clear(); } } catch (Exception e) { // 移动文件到Error,记录Log string strToPath = CpkErrorPath + "\\" + zipName; if (!Directory.Exists(strToPath)) { Directory.CreateDirectory(strToPath); } File.Move(file.FullName, strToPath + "\\" + file.Name); WriteSysLog(e.Message); } } DirectoryInfo[] allDir = dir.GetDirectories(); foreach (DirectoryInfo d in allDir) { TraverseFolder(d, act, cpks, zipName); } }
public void CpkExcute(string logFromPath, string logToPath, Action <FileInfo, List <Cpk>, string> transfaseDataTable) { double move = 0, read = 0, zip = 0, del = 0; var fileCount = 0; var total = CodeTimer.TimeLong("总共时间", 1, () => { string zipFileName = DateTime.Now.ToString("yyyyMMddHHmm"); string logToAbsPath = logToPath + "\\" + zipFileName; try { // ReSharper disable once AccessToModifiedClosure move = CodeTimer.TimeLong("移动数据", 1, () => FileUtil.MoveFolder(logFromPath, logToAbsPath, ref fileCount)); DirectoryInfo myFolder = new DirectoryInfo(logToAbsPath); List <Cpk> cpks = new List <Cpk>(); read = CodeTimer.TimeLong("解析数据", 1, () => { TraverseFolder(myFolder, transfaseDataTable, cpks, zipFileName); CpkRepository.BulkInsertAll(cpks); } ); zip = CodeTimer.TimeLong("压缩数据", 1, () => { if (FileUtil.Zip(logToAbsPath, logToAbsPath + ".zip", "")) { del = CodeTimer.TimeLong("删除数据", 1, () => FileUtil.DeleteDir(logToAbsPath)); } }); zip = zip - del; } catch (Exception e) { string log = "解析数据出错,原因:[" + e + "、消息:" + e.Message + "]"; Console.WriteLine(@" Err: "+ e.Message); WriteSysLog(log); } }); OperationLogRepository.Insert(new OperationLog() { Num = fileCount, MoveTimes = move, ReadTimes = read, ZipTimes = zip, DeleteTimes = del, TotalTimes = total, OperationType = "Cpks" }); }