private void scan(object pathname) { string dirPath = pathname.ToString(); SearchFile thisForm = (SearchFile)pathname; dirPath = thisForm.PathTextBox.Text; if (dirPath != null && dirPath.Length > 0) { if (Directory.Exists(dirPath)) { files.Clear(); List <string> Files = new List <string>(); List <string> Dirs = new List <string>(); Dirs.Add(dirPath); while (Dirs.Count > 0) { try { string searchDir = Dirs[0]; Dirs.RemoveAt(0); string[] file = Directory.GetFiles(searchDir); string[] dir = Directory.GetDirectories(searchDir); foreach (string item in file) { Files.Add(item); } foreach (string item in dir) { Dirs.Add(item); count++; } } catch (Exception ex) { ex.ToString(); } } //MessageBox.Show(count.ToString()); files.AddRange(Files); setListBoxEventHanlder up = new setListBoxEventHanlder(thisForm.setListBox); thisForm.BeginInvoke(up, thisForm); thisForm.Invoke(up, thisForm); thisForm.finish(thisForm); Thread.Sleep(1000); } } }
/* * 功能:扫描文件及其子文件 */ private void scan(object pathname) { string dirPath = pathname.ToString(); SearchFile thisForm = (SearchFile)pathname; dirPath = thisForm.PathTextBox.Text; if (dirPath != null && dirPath.Length > 0) { if (Directory.Exists(dirPath)) { files.Clear(); List <string> Files = new List <string>(); List <string> Dirs = new List <string>(); Dirs.Add(dirPath); while (Dirs.Count > 0) { try { string searchDir = Dirs[0]; Dirs.RemoveAt(0); string[] file = Directory.GetFiles(searchDir); string[] dir = Directory.GetDirectories(searchDir); foreach (string item in file) { Files.Add(item); } foreach (string item in dir) { Dirs.Add(item); } } catch { } } files.AddRange(Files); thisForm.finish(thisForm); } } }