protected override void OnScan() { recDepth = 0; BackgroundWorker worker = new BackgroundWorker(); Stopwatch timer = Stopwatch.StartNew(); worker.DoWork += (sender, args) => { DriveInfo[] drives = DriveInfo.GetDrives(); IEnumerable <DriveInfo> readyDrives = drives.Where(x => x.IsReady); Log.Message(string.Format("{0} ready drives detected", readyDrives.Count())); Parallel.ForEach(readyDrives, (DriveInfo drive) => { Log.Message(string.Format("Scanning drive {0}: [{1}] ASync...", drive.VolumeLabel, drive.Name)); SystemQueryResultItem result = new SystemQueryResultItem(drive.Name); AddItem(result); ScanSubDirsAndFiles(drive.RootDirectory, true); }); }; worker.RunWorkerCompleted += (sender, args) => { SortBigList(); ScanComplete(); Log.Success(string.Format("{0} scan completed in {1} seconds", GetType().Name, timer.Elapsed.TotalSeconds.ToString("F3"))); GC.Collect(); Log.ReportMemoryUsage(); Console.Beep(); }; worker.RunWorkerAsync(); }
private void AddItem(SystemQueryResultItem item) { if (item != null) { BigResultList.Add(item); } }
private void ScanSubDirsAndFiles(DirectoryInfo parentDir, bool isRoot = false) { // temp if (recDepth == 0) { Log.Warning(string.Format("Temporary using recursion depth limit ({0}) for debugging purposes", DebugOptions.SystemQueryDepthLimit)); } recDepth++; if (recDepth >= DebugOptions.SystemQueryDepthLimit) { return; } try { IEnumerable <DirectoryInfo> dirs = parentDir.EnumerateDirectories(); IEnumerable <FileInfo> files = parentDir.EnumerateFiles(); // Root folders are filtered and scanned in parallel. if (isRoot) { dirs = dirs.Where(x => !RootIgnorePaths.Contains(x.FullName)); Parallel.ForEach(dirs, (DirectoryInfo dir) => { Stopwatch timer = Stopwatch.StartNew(); ScanSubDirsAndFiles(dir); SystemQueryResultItem result = new SystemQueryResultItem(dir.FullName); AddItem(result); }); } else { foreach (DirectoryInfo dir in dirs) { ScanSubDirsAndFiles(dir); SystemQueryResultItem result = new SystemQueryResultItem(dir.FullName); AddItem(result); } } // Scan files. foreach (FileInfo file in files) { SystemQueryResultItem result = new SystemQueryResultItem(file.FullName); AddItem(result); } } catch (UnauthorizedAccessException e) { //Log.Warning(e.Message); } }