public void Start(TreeView _tView, string rootDir, string _mask) { End(); stopwatch = new System.Diagnostics.Stopwatch(); DirectoryInfo dir = new DirectoryInfo(rootDir); TreeNode node = new TreeNode(dir.Name, 0, 0); if (dir.Exists) { Continue(); numOfAll = 0; GUIController.UpdateTotalFilesLabel(numOfAll); numOfFound = 0; GUIController.UpdateFoundFilesLabel(numOfFound); fileNameMask = _mask; tView = _tView; tView.Nodes.Clear(); tView.Nodes.Add(node); thread = new Thread(() => Work(dir, node)); thread.Start(); } else { MessageBox.Show("Проверьте парвильность введённых данных", "Ошибка"); } }
private void Work(DirectoryInfo rootDir, TreeNode rootNode) { try { while (stopRunning) { } // Контроль остановки поиска TreeNode node; Regex regex = new Regex(fileNameMask); DirectoryInfo[] subDirs = rootDir.GetDirectories(); GUIController.UpdateCurrentFolderLabel(rootDir.Name); // Проход всех подпапок в главной папке foreach (DirectoryInfo dir in subDirs) { node = new TreeNode(dir.Name, 0, 0) { Tag = rootDir }; if (subDirs.Length != 0) { Work(dir, node); } ControlHelper.InvokeEx(tView, new Action(() => rootNode.Nodes.Add(node))); } // Проход по всем полученным файлам и применение к ним регекса foreach (FileInfo file in rootDir.GetFiles()) { if (regex.Match(file.Name).Success) { numOfFound++; node = new TreeNode(file.Name, 1, 1); ControlHelper.InvokeEx(tView, new Action(() => rootNode.Nodes.Add(node))); GUIController.UpdateFoundFilesLabel(numOfFound); } numOfAll++; GUIController.UpdateTotalFilesLabel(numOfAll); } } catch (Exception ex) { if (ex is UnauthorizedAccessException) { return; } } }