搜索分区中的特定文件 [英] Search for specific files in a partition

查看:69
本文介绍了搜索分区中的特定文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在硬盘分区中搜索* .lnk文件。如何使用C#执行此操作?

I want to search for *.lnk files in a hard disk partition. How can I do this using C#?

推荐答案

您无法在所有分区中搜索文件,因为并非所有分区都具有文件系统或者用户可以访问。你实际上更有可能需要搜索中的文件。



所有你需要的就是找到文件从卷的根目录开始递归。您只需要这种方法:

http://msdn.microsoft.com /en-us/library/07wt70x2.aspx [ ^ ]。



但是,您需要了解一个令人讨厌的微软问题。请参阅我过去的回答: Directory.Get.Files搜索模式问题 [ ^ ]。



要查找系统中的所有卷,您可以使用WMI。请看我过去的答案:

在c#中获取Pendrive实例处理程序ID [ ^ ]。



由于这个答案只是关于可移动驱动器,你应该删除<$我显示的查询中的c $ c> WHERE 子句。



-SA
You cannot search for a file in all partitions, because not all partitions even have file systems or accessible to a user. It''s more likely that you actually need to search for files in a volume.

All you need them is to find the file recursively starting from a root directory of a volume. All you need is this method:
http://msdn.microsoft.com/en-us/library/07wt70x2.aspx[^].

However, there is a nasty Microsoft problem you need to know about. Please see my past answer: Directory.Get.Files search pattern problem[^].

To find all volumes in the system, you can use WMI. Please see my past answer:
Get Pendrive Instance Handler ID in c#[^].

As this answer was about removable drives only, you should remove WHERE clause from the query I show.

—SA


理论上它很简单:

In theory it''s pretty easy:
string[] paths = Directory.GetFiles(@"D:\Temp", "*.lnk", SearchOption.AllDirectories);

但除非你的应用程序具有完全访问权限,否则几乎肯定会失败 - 这是不可能的。

在这种情况下,请阅读根目录Directory.GetDirectories,然后使用上面的代码在 try ... catch 块中读取所有这些代码,用每个drirectory名称替换我使用的固定路径。然后,您可以忽略尝试访问回收站时引发的异常!

But that will almost certainly fail unless your app has full access privileges - which it is not likely to have.
In that case, read the root directory with Directory.GetDirectories, then read all of them with the above code in a try...catch block, substituting each drirectory name for the fixed path I used. You can then ignore the exception that is thrown when you try to access the recycle bin!


这篇关于搜索分区中的特定文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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