[转]C#搜索抓取本地硬盘文件

问题描述:譬如说要搜索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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值