问题描述:譬如说要搜索d:盘中的所有后缀为html的文件。这里只是简单实现一个抓取的类,大家可以根据需要自行扩展关键是用到一个递归函数,依此搜索子文件夹下的文件,按照时间保存
代码 public class SearchBase{ private SearchBase() { } static int ra; public static void addSubDirectory(DirectoryInfo directory, string pattern) { foreach (FileInfo fi in directory.GetFiles(pattern)) { addrelativeDocument(fi.FullName); } foreach (DirectoryInfo di in directory.GetDirectories()) { addSubDirectory(di, pattern); } } public static void addrelativeDocument( string path) { string html; using (StreamReader sr = new StreamReader(path, System.Text.Encoding.Default)) { html = sr.ReadToEnd(); } string time = DateTime.Now.ToLongTimeString(); string [] timetopath = time.Split( ' : ' ); string newtime = null ; for ( int i = 0 ; i < timetopath.Length; i ++ ) { newtime += timetopath[i].ToString(); } newtime += " ( " + ra + " ) " ; string p = @" f:files " + newtime + " .txt " ; if ( ! File.Exists(p)) { FileStream fs = new FileStream(p, FileMode.Create, FileAccess.Write); StreamWriter sw = new StreamWriter(fs); sw.Write(html); sw.Close(); fs.Close(); } ra ++ ; }}
转载于:https://www.cnblogs.com/chinaHunk/articles/1890520.html
相关资源:全盘遍历查找文件(多线程)