Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Visual-Studio C#4.0
I want to search for *.lnk files in a hard disk partition. How can I do this using C#?
Posted 10-Feb-13 3:05am
Rate this: bad
good
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:
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
  Permalink  
Rate this: bad
good
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!
  Permalink  

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

  Print Answers RSS
0 BillWoodruff 300
1 Mathew Soji 274
2 Afzaal Ahmad Zeeshan 268
3 DamithSL 225
4 Sergey Alexandrovich Kryukov 205
0 OriginalGriff 6,249
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 3,865


Advertise | Privacy | Mobile
Web02 | 2.8.1411019.1 | Last Updated 10 Feb 2013
Copyright © CodeProject, 1999-2014
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