我如何...在所有驱动器和所有目录中搜索.dox文件 [英] How do I...search a .dox file in all the drive and all the directories

查看:128
本文介绍了我如何...在所有驱动器和所有目录中搜索.dox文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想搜索所有驱动器和目录中的文件。下面是我的代码,但是当我尝试SearchOption时。 TopDirectoryOnly它可以工作但是在AllDirectories中它产生异常(希望有一些访问被拒绝的目录。

请帮助如何跳过这些目录或任何其他方式...

I want to search a file in all drive and directories. Below is my code but when I try to SearchOption. TopDirectoryOnly it works but in AllDirectories it generate the exception (Hope there are some access denied directories.
Please help how to skip these directories or any other way …

DriveInfo[] allDrive = DriveInfo.GetDrives();

foreach (DriveInfo d in allDrive)
{
    string b = d.Name.ToString();
     var filename = Directory.GetFiles(b, "*.doc*", SearchOption.AllDirectories);

        foreach (string a in filename)
        {
          listBox1.Items.Add(a.ToString());
          listBox1.Items.Add("Creation Time:"+System.IO.File.GetCreationTime(a.ToString()));
        }
}

推荐答案

你几乎不能在没有访问违规的情况下在根文件夹上使用AllDirectories - 除非你拥有完全的管理员权限,这对于简单的文件搜索来说相当过分!



你唯一的选择就是在try中使用递归文件夹检查... catch块,并单独检查驱动器上的每个文件夹,然后查看其子文件夹,前提是您可以访问它们。
You can't use AllDirectories on the root folder without almost certainly getting access violations - unless you have full admin privileges, which is rather overkill for a simple file search!

Your only option is to use a recursive folder check inside a try...catch block, and check each folder on the drive individually, followed by its subfolders provided you have access to them.


这篇关于我如何...在所有驱动器和所有目录中搜索.dox文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆