private void bgWorker_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker bg = sender as BackgroundWorker; string path = (string)e.Argument; try { this.ProcessDirectory(path, bg, e); } catch (Exception ex) { // Cancel the current search StopSearch(); ExceptionForm exfm = new ExceptionForm(ex, m_Version); exfm.ShowDialog(this); } }
private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { try { m_Stopwatch.Stop(); m_SearchSuccesful = false; if (!e.Cancelled) { AddParentDirectoryItem(); if (m_DirectorySizes.Count == 0) { long size = AddCurrentDirectoryFiles(m_PathBox.Text); //AddFilesToCache(); UpdateWithStopwatch(false); BuildTextStatistics(size); CollectPieChartData(); m_SearchSuccesful = true; m_PieChart.Refresh(); } else { List <string> subfolders = new List <string>(); long totalSize = 0; foreach (KeyValuePair <string, long> kvp in m_DirectorySizes) { string subfolder = kvp.Key; long size = kvp.Value; // Remove start slash and initial path subfolder = subfolder.Replace(m_PathBox.Text, ""); if (subfolder.StartsWith("\\")) { subfolder = subfolder.Remove(0, 1); } // Check if it's a direct subfolder bool matched = false; foreach (string sf in subfolders) { Match m = Regex.Match(subfolder, Regex.Escape(sf + "\\")); //Match m = Regex.Match(subfolder, sf); if (m.Success) { matched = true; } } // Not a subfolder's subfolder, so we can add it to the list of current directories if (!matched) { subfolders.Add(subfolder); AddDirectoryToList(subfolder, kvp.Key, size); totalSize += size; } } totalSize += AddCurrentDirectoryFiles(m_PathBox.Text); UpdateWithStopwatch(false); BuildTextStatistics(totalSize); m_SearchSuccesful = true; m_DirectorySizes.Clear(); m_MaxDirs = (int)(((m_PieChart.Size.Height * 0.75)) / 10); CollectPieChartData(); AddFilesToCache(); m_PieChart.Refresh(); } m_AnalyzeButton.Text = m_strStartAnalysis; } } catch (Exception ex) { StopSearch(); ExceptionForm exfm = new ExceptionForm(ex, m_Version); exfm.ShowDialog(this); } }
private void ProcessDirectory(string path, BackgroundWorker worker, DoWorkEventArgs e) { if (!System.IO.Directory.Exists(path)) { throw new System.IO.DirectoryNotFoundException("Неверный путь"); } // Check if we can access the path try { FileIOPermission perm = new FileIOPermission(FileIOPermissionAccess.Read, path); } catch (System.Security.SecurityException) { worker.CancelAsync(); } try { if (worker.CancellationPending) { e.Cancel = true; } else { string[] entries = Directory.GetDirectories(path); foreach (string currentDir in entries) { // Check for errors // - Skip hidden files // - Skip directories that don't exist // - Skip directories that we don't have access to // We do this by asking read permission for this directory. If it gets denied, then we skip it if ((File.GetAttributes(currentDir) & FileAttributes.Hidden) == FileAttributes.Hidden) { continue; } if (!Directory.Exists(currentDir)) // Should never occur. Just in case. { continue; } long length = 0; try { length = GetDirectoryFileSize(currentDir); if (!m_DirectorySizes.ContainsKey(currentDir)) { m_DirectorySizes.Add(currentDir, length); } } // avoid all IO, security and user access exceptions - if we can't access something catch (Exception ex) { continue; } DirectoryInfo parent = new DirectoryInfo(currentDir).Parent; // Since we have to take the size of sub-folders into account the algorithm // must iterate backwards towards the parent directories and add the size of the current // directory to it. while (parent != null && !this.bgWorker.CancellationPending) { if (m_DirectorySizes.ContainsKey(parent.FullName)) { m_DirectorySizes[parent.FullName] += length; parent = parent.Parent; } else { break; } } ProcessDirectory(currentDir, worker, e); } } } catch (Exception ex) { StopSearch(); ExceptionForm exfm = new ExceptionForm(ex, m_Version); exfm.ShowDialog(this); } }
private void ProcessDirectory(string path, BackgroundWorker worker, DoWorkEventArgs e) { if (!System.IO.Directory.Exists(path)) throw new System.IO.DirectoryNotFoundException("Invalid path"); // Check if we can access the path try { FileIOPermission perm = new FileIOPermission(FileIOPermissionAccess.Read, path); } catch (System.Security.SecurityException) { worker.CancelAsync(); } try { if (worker.CancellationPending) { e.Cancel = true; } else { string[] entries = Directory.GetDirectories(path); foreach (string currentDir in entries) { // Check for errors // - Skip hidden files // - Skip directories that don't exist // - Skip directories that we don't have access to // We do this by asking read permission for this directory. If it gets denied, then we skip it if ((File.GetAttributes(currentDir) & FileAttributes.Hidden) == FileAttributes.Hidden) continue; if (!Directory.Exists(currentDir)) // Should never occur. Just in case. continue; long length = 0; try { length = GetDirectoryFileSize(currentDir); if (!m_DirectorySizes.ContainsKey(currentDir)) m_DirectorySizes.Add(currentDir, length); } // avoid all IO, security and user access exceptions - if we can't access something catch (Exception ex) { continue; } DirectoryInfo parent = new DirectoryInfo(currentDir).Parent; // Since we have to take the size of sub-folders into account the algorithm // must iterate backwards towards the parent directories and add the size of the current // directory to it. while (parent != null && !this.bgWorker.CancellationPending) { if (m_DirectorySizes.ContainsKey(parent.FullName)) { m_DirectorySizes[parent.FullName] += length; parent = parent.Parent; } else break; } ProcessDirectory(currentDir, worker, e); } } } catch (Exception ex) { StopSearch(); ExceptionForm exfm = new ExceptionForm(ex, m_Version); exfm.ShowDialog(this); } }
private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { try { m_Stopwatch.Stop(); m_SearchSuccesful = false; if (!e.Cancelled) { AddParentDirectoryItem(); if (m_DirectorySizes.Count == 0) { long size = AddCurrentDirectoryFiles(m_PathBox.Text); //AddFilesToCache(); UpdateWithStopwatch(false); BuildTextStatistics(size); CollectPieChartData(); m_SearchSuccesful = true; m_PieChart.Refresh(); } else { List<string> subfolders = new List<string>(); long totalSize = 0; foreach (KeyValuePair<string, long> kvp in m_DirectorySizes) { string subfolder = kvp.Key; long size = kvp.Value; // Remove start slash and initial path subfolder = subfolder.Replace(m_PathBox.Text, ""); if (subfolder.StartsWith("\\")) subfolder = subfolder.Remove(0, 1); // Check if it's a direct subfolder bool matched = false; foreach (string sf in subfolders) { Match m = Regex.Match(subfolder, Regex.Escape(sf + "\\")); //Match m = Regex.Match(subfolder, sf); if (m.Success) matched = true; } // Not a subfolder's subfolder, so we can add it to the list of current directories if (!matched) { subfolders.Add(subfolder); AddDirectoryToList(subfolder, kvp.Key, size); totalSize += size; } } totalSize += AddCurrentDirectoryFiles(m_PathBox.Text); UpdateWithStopwatch(false); BuildTextStatistics(totalSize); m_SearchSuccesful = true; m_DirectorySizes.Clear(); m_MaxDirs = (int)(((m_PieChart.Size.Height * 0.75)) / 10); CollectPieChartData(); AddFilesToCache(); m_PieChart.Refresh(); } m_AnalyzeButton.Text = m_strStartAnalysis; } } catch (Exception ex) { StopSearch(); ExceptionForm exfm = new ExceptionForm(ex, m_Version); exfm.ShowDialog(this); } }