private void backgroundWorker_DoWork(CancellationToken ct) { try { String iecachepath = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache); log.Info("IE cache path: " + iecachepath); var totalfiles = SafeFileEnumerator.EnumerateFiles(iecachepath, "*.*", SearchOption.AllDirectories); //Directory.EnumerateFiles(iecachepath, "*.*", SearchOption.AllDirectories).Where<String>(s => (s.EndsWith(".jpg") || s.EndsWith(".png"))); string roamingPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); var dirs = SafeFileEnumerator.EnumerateDirectories(roamingPath, "*Cache*", SearchOption.AllDirectories); string localAppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); dirs = dirs.Concat(SafeFileEnumerator.EnumerateDirectories(localAppDataPath, "*Cache*", SearchOption.AllDirectories)); foreach (var dir in dirs) { totalfiles = totalfiles.Concat(SafeFileEnumerator.EnumerateFiles(dir, "*.*", SearchOption.AllDirectories)); } int tcount = totalfiles.Count(); int num = 0; foreach (var file in totalfiles) { log.Info("Scan: " + file); PornClassifier.ImageType t; try { t = PornClassifier.Instance.Classify(file); } catch (Exception exception) { log.Error(exception.ToString()); t = PornClassifier.ImageType.Normal; } num++; //backgroundWorker.ReportProgress(100 * num / tcount, new PornFile(file, t)); //progressBar.Invoke(backgroundWorker_ProgressChanged, (int)100*num/tcount, file, t); int percentage = 100 * num / tcount; if (percentage > 100) { percentage = 100; } progressBar.BeginInvoke(progressChanged, percentage, file, t); if (ct.IsCancellationRequested) { ct.ThrowIfCancellationRequested(); } //if (backgroundWorker.CancellationPending) // break; } } catch (Exception exception) { log.Error(exception.ToString()); } }
//扫描本地浏览器缓存图片文件 void FastLocalScan(CancellationToken ct, ManualResetEvent pauseEvent, IProgress <ScanProgress> progress, DateTime lastDateTime) { try { String iecachepath = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache); log.Info("IE cache path: " + iecachepath); //var totalfiles = Directory.EnumerateFiles(iecachepath, "*.*", SearchOption.AllDirectories).Where<String>(s => (s.EndsWith(".jpg") || s.EndsWith(".png"))); //var totalfiles = SafeFileEnumerator.EnumerateFiles(iecachepath, "*.*", SearchOption.AllDirectories).Where<String>(ImageFilesCanProcessed); //var totalfiles = SafeFileEnumerator.EnumerateFileInfos(iecachepath, "*.*", SearchOption.AllDirectories).Where<FileInfo>(ImageFilesCanProcessed); //var totalfiles = SafeFileEnumerator.EnumerateFileInfos(iecachepath, "*.*", SearchOption.AllDirectories).Where<FileInfo>(ImageFileInfosCanProcessed); var totalfiles = Enumerable.Empty <FileInfo>(); //此处不能使用directoryfilter,因为该filter是控制对那些dir枚举其下的dir,而不是文件 //这会导致在win8下,ie的缓存目录为INetCache,该目录下的文件会被重复枚举并扫描 //string homePath = (Environment.OSVersion.Platform == PlatformID.Unix || // Environment.OSVersion.Platform == PlatformID.MacOSX) // ? Environment.GetEnvironmentVariable("HOME") // : Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%"); string homePath = Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%"); var dirs = SafeFileEnumerator.EnumerateDirectories(homePath, "*Cache*", SearchOption.AllDirectories).Where <String>(s => !s.Contains(iecachepath)); //string roamingPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); //var dirs = SafeFileEnumerator.EnumerateDirectories(roamingPath, "*Cache*", SearchOption.AllDirectories).Where<String>(s=>!s.Contains(iecachepath)); //string localAppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); //dirs = dirs.Concat(SafeFileEnumerator.EnumerateDirectories(localAppDataPath, "*Cache*", SearchOption.AllDirectories).Where<String>(s=>!s.Contains(iecachepath))); List <string> cachedirs = dirs.ToList(); List <string> tmpcachedirs = cachedirs; cachedirs.Add(iecachepath); //剔除路径重叠的目录,留下最高层的目录,防止重复扫描 cachedirs.RemoveAll(s => tmpcachedirs.Exists(ss => ss != s && s.Contains(ss))); //枚举每个目录内的文件 foreach (var dir in cachedirs) { log.Info("Fast dir: " + dir); totalfiles = totalfiles.Concat(SafeFileEnumerator.EnumerateFileInfos(dir, "*.*", SearchOption.AllDirectories)); } sBrowserDirList = cachedirs; //sBrowserDirList= dirs.ToList(); //sBrowserDirList.Add(iecachepath); //var files = GetFiles("C:\\SearchDirectory", d => !d.Contains("AvoidMe", StringComparison.OrdinalIgnoreCase), "*.*"); //if(Properties.Settings.Default.isFastLocalScanIncremental) // totalfiles = totalfiles.Where<FileInfo>(finfo => finfo.CreationTime > Properties.Settings.Default.lastFastLocalScanDateTime); if (lastDateTime != DateTime.MinValue) { totalfiles = totalfiles.Where <FileInfo>(finfo => finfo.CreationTime > lastDateTime); } int tcount = totalfiles.Count(); int num = 0; foreach (var file in totalfiles) { AnalysisFile(file.FullName, ct, pauseEvent, progress, num, tcount, true); num++; } } catch (Exception exception) { log.Error(exception.ToString()); } }
//所有本地文件扫描,排除了浏览器缓存文件以防重复扫描 void AllLocalFileScan(CancellationToken ct, ManualResetEvent pauseEvent, IProgress <ScanProgress> progress, DateTime lastDateTime) { try { DriveInfo[] allDrives = DriveInfo.GetDrives(); //List<string> allTopDirs = new List<string>(); //先扫描所有磁盘根目录下的文件 IEnumerable <string> allTopDirs = Enumerable.Empty <string>(); foreach (var drive in allDrives) { if (drive.DriveType == DriveType.Fixed) { allTopDirs = allTopDirs.Concat(SafeFileEnumerator.EnumerateDirectories( drive.RootDirectory.FullName, "*", SearchOption.TopDirectoryOnly, null )); //var files = SafeFileEnumerator.EnumerateFiles(drive.RootDirectory.FullName, "*.*", SearchOption.TopDirectoryOnly, null).Where<String>(FilesCanProcessed); var fileinfos = SafeFileEnumerator.EnumerateFileInfos(drive.RootDirectory.FullName, "*.*", SearchOption.TopDirectoryOnly, null).Where <FileInfo>(finfo => FilesCanProcessed(finfo.Name)); if (lastDateTime != DateTime.MinValue) { fileinfos = fileinfos.Where <FileInfo>(finfo => finfo.CreationTime > lastDateTime); } //if (Properties.Settings.Default.isAllLocalScanIncremental) // fileinfos = fileinfos.Where<FileInfo>(finfo => finfo.CreationTime > Properties.Settings.Default.lastAllLocalScanDateTime); foreach (FileInfo finfo in fileinfos) { AnalysisFile(finfo.FullName, ct, pauseEvent, progress, 0, 1, false); } } } String iecachepath = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache); string localAppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); List <string> allTopDirsList = allTopDirs.ToList(); int tcount = allTopDirsList.Count; int num = 0; //再分别扫描所有磁盘下根文件夹下所有文件 foreach (var topdir in allTopDirsList) { //var allfiles = SafeFileEnumerator.EnumerateFiles(topdir, "*.*", SearchOption.AllDirectories, // d => !(d.Contains(iecachepath) || d.Contains(localAppDataPath)) // ).Where<String>(FilesCanProcessed); //排除浏览器缓存搜索的范围 string homePath = Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%"); //var allFileInfos = SafeFileEnumerator.EnumerateFileInfos(topdir, "*.*", SearchOption.AllDirectories, // d => !(d.Contains(iecachepath) || d.Contains(homePath)) // ).Where<FileInfo>(finfo => FilesCanProcessed(finfo.Name)); var allFileInfos = SafeFileEnumerator.EnumerateFileInfos(topdir, "*.*", SearchOption.AllDirectories, removeBrowserDirs ).Where <FileInfo>(finfo => FilesCanProcessed(finfo.Name)); if (lastDateTime != DateTime.MinValue) { allFileInfos = allFileInfos.Where <FileInfo>(finfo => finfo.CreationTime > lastDateTime); } //if (Properties.Settings.Default.isAllLocalScanIncremental) // allFileInfos = allFileInfos.Where<FileInfo>(finfo => finfo.CreationTime > Properties.Settings.Default.lastAllLocalScanDateTime); foreach (FileInfo finfo in allFileInfos) { AnalysisFile(finfo.FullName, ct, pauseEvent, progress, num, tcount, false); } num++; } } catch (Exception ex) { log.Error(ex.ToString()); } }