Click here to Skip to main content
12,948,385 members (65,159 online)
Rate this:
Please Sign up or sign in to vote.
I want to search for *.lnk files in a hard disk partition. How can I do this using C#?
Posted 10-Feb-13 2:05am
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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:[^].

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.

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

In theory it's pretty easy:
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!

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 5,344
CHill60 3,275
Maciej Los 2,703
Jochen Arndt 1,935
ppolymorphe 1,795

Advertise | Privacy | Mobile
Web01 | 2.8.170524.1 | Last Updated 10 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100