/// <summary> /// Gets a quantitative percent, of the tally specified in relation to the other. /// </summary> public double GetPercent(FileTally tally) { return((double)tally.Count / tallies.Sum(t => t.Count) * 100); }
// I found stack based traversal with the static enumerate methods to be the most efficient. /// <summary> /// Walk a directory tree with stack based traversal. /// </summary> /// <param name="root">The top-level directory to start at.</param> private void StackTraverseDirectory(string root) { var dirs = new Stack <string>(20); dirs.Push(root); while (dirs.Count > 0) { string currentDir = dirs.Pop(); IEnumerable <string> subDirs, fileNames; dirsIterated++; try { subDirs = Directory.EnumerateDirectories(currentDir); fileNames = Directory.EnumerateFiles(currentDir); } catch (UnauthorizedAccessException) { errorCount++; continue; } foreach (string fileName in fileNames) { // Check for cancellation every file iteration if (WorkerSupportsCancellation && CancellationPending) { return; } filesIterated++; // Progress reporting if (WorkerReportsProgress) { if (reportTracker == reportProgressFreq) { reportTracker = 0; ReportProgress(0, filesIterated); } else { reportTracker++; } } // Tallying string ext = Path.GetExtension(fileName).ToLower(); var tally = tallies.Find(t => t.Extension == ext); // returns null if not found? if (tally == null) { tallies.Add(new FileTally(ext)); } else { FileTally.Increment(tally); } } foreach (string dir in subDirs) { dirs.Push(dir); } } }