public static void Start() { try { Console.WriteLine(string.Format("开始扫描 {0}", DateTime.Now.ToLongTimeString())); var drivers = Environment.GetLogicalDrives().ToList(); List <FileInfo> fi = new List <FileInfo>(); List <Match> temp = new List <Match>(); List <Match> matches = new List <Match>(); foreach (var driver in drivers) { Console.WriteLine(string.Format("添加扫描驱动器: {0}", driver)); if (Directory.Exists(driver + "\\fin\\")) { if (!string.IsNullOrEmpty(FileUtility.GetFilesRecursive(driver + "\\fin\\", formats, excludes, fi, 100))) { Console.WriteLine("获取文件失败"); } } } var avs = JavDataBaseManager.GetAllAV(); avs.ForEach(x => x.ID = x.ID.ToUpper()); var prefix = FileUtility.GetPrefix(avs); Console.WriteLine(string.Format("获取AV {0} 条, 前缀 {1} 条", avs.Count, prefix.Count)); Parallel.ForEach(fi, new ParallelOptions { MaxDegreeOfParallelism = 10 }, file => { var split = file.Name.Split('-'); if (split.Length >= 3) { var id = split[0].ToUpper() + "-" + split[1].ToUpper(); var matchedAvs = avs.Where(x => x.ID == id).ToList(); if (matchedAvs != null) { Console.WriteLine(string.Format("{0} 找到 {1} 个匹配AV", file.FullName, matchedAvs.Count)); foreach (var av in matchedAvs) { Match m = new Match { AvID = id, AvName = av.Name, Location = file.DirectoryName, Name = file.Name, MatchAVId = av.AvId }; matches.Add(m); } } } }); Console.WriteLine("清空匹配记录"); ScanDataBaseManager.ClearMatch(); foreach (var match in matches) { try { //Console.WriteLine("保存 " + match.AvID); ScanDataBaseManager.SaveMatch(match); } catch (Exception ee) { //MessageBox.Show(match.Location + match.Name); //MessageBox.Show(ee.ToString()); } } } catch (Exception e) { Console.WriteLine(e.ToString()); } finally { Console.WriteLine(string.Format("扫描结束 {0}", DateTime.Now.ToLongTimeString())); } }
public static void Start() { try { sb.AppendLine(string.Format("开始扫描 {0}", DateTime.Now.ToLongTimeString())); //var drivers = Environment.GetLogicalDrives().Skip(1).ToList(); List <FileInfo> fi = new List <FileInfo>(); List <Match> temp = new List <Match>(); foreach (var driver in folders) { sb.AppendLine(string.Format("添加扫描驱动器: {0}", driver)); Console.WriteLine("Processing " + driver); if (!string.IsNullOrEmpty(FileUtility.GetFilesRecursive(driver, formats, excludes, fi, 100))) { sb.AppendLine("获取文件失败"); } } var avs = JavDataBaseManager.GetAllAV(); var prefix = FileUtility.GetPrefix(avs); Console.WriteLine("Fi -> " + fi.Count); Console.WriteLine("AV -> " + avs.Count + " Prefix -> " + prefix.Count); sb.AppendLine(string.Format("符合条件文件: {0}, 总共AV: {1}", fi.Count, avs.Count)); foreach (var file in fi) { var scan = new Scan { FileName = file.Name.Trim().ToUpper(), Location = file.DirectoryName.Trim().ToLower(), Size = FileSize.GetAutoSizeString(file.Length, 2) }; var possibleIDs = FileUtility.GetPossibleID(scan, prefix); possibleIDs = possibleIDs.OrderByDescending(x => x.Length).Take(1).ToList(); sb.AppendLine(string.Format("文件{0}可能的Match有{1}", file, possibleIDs.Count)); Console.WriteLine("PossibleIDs -> " + possibleIDs.Count); AddTemp(scan, possibleIDs, temp); } Console.WriteLine("Temp -> " + temp.Count); sb.AppendLine(string.Format("一共找到{0}个Match", temp.Count)); ScanDataBaseManager.ClearMatch(); foreach (var m in temp) { Console.WriteLine(string.Format("Insert {0}\\{1}", m.Location, m.Name)); sb.AppendLine(string.Format("在库中添加Match -> {0}", m.Location)); ScanDataBaseManager.SaveMatch(m); } sb.AppendLine("更新数据库状态"); ScanDataBaseManager.InsertFinish(); var duplicateItemList = new List <DuplicateItem>(); var tempDic = temp.GroupBy(x => x.AvID.ToLower()).ToDictionary(x => x.Key, y => y.ToList()); foreach (var item in tempDic) { var tempItem = new DuplicateItem(); tempItem.AvId = item.Key; tempItem.Matches = item.Value; duplicateItemList.Add(tempItem); } var jsonRoot = "C:/AvLog/"; var jsonStr = JsonConvert.SerializeObject(duplicateItemList).Replace("\\", "\\\\"); var jsonFile = "ScanJson" + DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss") + ".json"; LogHelper.WriteLog(jsonFile, jsonStr); EmailHelper.SendEmail("ScanJson", "详情见附件", new[] { "*****@*****.**" }, new[] { jsonRoot + jsonFile }); } catch (Exception e) { sb.AppendLine(e.ToString()); } finally { sb.AppendLine(string.Format("扫描结束 {0}", DateTime.Now.ToLongTimeString())); //var root = "C:/AvLog/"; //var file = "ScanAndMatch" + DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss") + "-log.txt"; //LogHelper.WriteLog(file, sb.ToString()); //EmailHelper.SendEmail("ScanAndMatchLog", "详情见附件", new[] { "*****@*****.**" }, new[] { root + file }); } }