文件搜索的实现(深度搜索)

类别:.NET开发 点击:0 评论:0 推荐:
System.Collections.Stack stackFile = new Stack(); /// /// 把要搜索的路径的文件夹全部压栈 /// ///

public Stack FileList(string path) { string[] files = System.IO.Directory.GetDirectories(path); foreach(string file in files) { stackFile.Push(file); FileList(file); } return stackFile; } /// /// 把所有符合搜索条件的文件放到一个ArrayList里 /// ///

搜索的路径 ///

要搜索的文件 /// public ArrayList SearchFile(string path,string filter) { ArrayList array = new ArrayList(); Stack stack = new Stack(); //-----------对根目录进行搜索 string[] Rootfiles = System.IO.Directory.GetFileSystemEntries(path,filter); foreach(string file in Rootfiles) { System.IO.DirectoryInfo dirInfo = new DirectoryInfo(file); array.Add(dirInfo); } //----------- stack = this.FileList(path); if(stack.Count > 0) { while(stack.Count > 0) { string CurrentFile = stack.Pop().ToString(); string[] files = System.IO.Directory.GetFileSystemEntries(CurrentFile,filter); foreach(string file in files) { System.IO.DirectoryInfo dirInfo = new DirectoryInfo(file); array.Add(dirInfo); } } } return array; }

本文地址:http://com.8s8s.com/it/it41571.htm