我想找到d盘里面所有 "*.pst文件,类似 windows 下的磁盘搜索功能,
using system.io;
directory.getfiles(@"d:\", "*.pst", searchoption.alldirectories)
测试环境为win7 提示说 某某目录没有访问权限。
获得文件属性 file.getattributes(dir.fullname).tostring()
没有权限访问的将会有 system 、hidden属性。
于是想到是否可以通过属性来过滤这些文件,因此想到了用递归的思想去遍历所有文件。
还好,在网上居然找到了这个。
using system.collections;
arraylist alist = new
arraylist();
void dirsearch(string
sdir)
{
try
foreach (string d in
directory.getdirectories(sdir))
foreach (string f in directory.getfiles(d,
"*.pst"))
alist.add(f);
}
dirsearch(d);
catch
(system.exception
excpt)
console.writeline(excpt.message);
}
getfiletime("d:\\");
通过递归和直接 找所有的文件,两者都有异常, 区别在于前者的异常,就抛出某个节点的异常, alist.add(f);
不会执行,(这里是递归循环哦)循环继续,后者是遇到异常,则赋值操作不能执行,返回null.
也可以通过getattributes 来判断,原理类似。