Click here to Skip to main content
15,895,557 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
I want to search for *.lnk files in a hard disk partition. How can I do this using C#?
Posted

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
 
Share this answer
 
In theory it's pretty easy:
C#
string[] paths = Directory.GetFiles(@"D:\Temp", "*.lnk", SearchOption.AllDirectories);
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!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900