日期:2014-05-18 浏览次数:20717 次
List<string> files = new List<string>(); void Add(string str) { lock (this) { files.Add(str); } } int count = 0; private void button2_Click(object sender, EventArgs e) { files.Clear(); DriveInfo c = new DriveInfo("c"); Stopwatch sw = new Stopwatch(); sw.Start(); ThreadPool.SetMaxThreads(50, 50); var arr = c.RootDirectory.GetDirectories(); count = arr.Length; foreach (var dir in arr) { ThreadPool.QueueUserWorkItem(getFolder2, dir); } while (count > 0) { Application.DoEvents(); Thread.Sleep(100); } sw.Stop(); var s = sw.ElapsedMilliseconds.ToString() + " ms"; MessageBox.Show(s); MessageBox.Show(files.Count.ToString()); foreach (var item in files) { MessageBox.Show(item); } } void getFolder2(object folder) { Queue<DirectoryInfo> dirs = new Queue<DirectoryInfo>(50); dirs.Enqueue((DirectoryInfo)folder); while (dirs.Count > 0) { var dir = dirs.Dequeue(); try { foreach (var item in dir.GetFiles("data.mdb", SearchOption.AllDirectories)) { Add(item.FullName); } continue; } catch (Exception) { } IEnumerable<DirectoryInfo> em ; try { em = dir.EnumerateDirectories("*", SearchOption.TopDirectoryOnly); } catch (Exception) { continue; } foreach (var d in em) { try { d.EnumerateFiles(); dirs.Enqueue(d); } catch (Exception) { } } } lock (this) { count--; } }
------解决方案--------------------
rivate void GetFileList(DirectoryInfo diroot)
{
foreach (FileInfo fileName in diroot.GetFiles())
{
listFile.Add(fileName.FullName);
}
foreach (DirectoryInfo dirSub in diroot.GetDirectories())
{
GetFileList(dirSub);
}
}
------解决方案--------------------