Directory Freebies VS CheatSheet Forum

RSS

Email

Translate

Home About Archive Privacy Contact Advertise Write for Dev102
Posted by Amit on Jul 15th, 2008 | Filed under .Net, C# | 16 Comments

Here is something neat I found out.

Say you are writing an application and one of the requirements is to allow File System search. You could always start using loops and such. I thought to myself why not do it in LINQ? I played around with it and in fact it is not so hard.

Lets see how it is done. Here is method that allows finding a specific file name in side a directory.

   1: private List SearchFilesByName(string DirectoryPath, string FileName) 
   2: { 
   3:     return (from file in new DirectoryInfo(DirectoryPath).GetFiles() 
   4:             where file.Name == FileName select file).ToList(); 
   5: }

Basically we Query the FileInfo[] which is returned from the GetFiles() method and compare the file name.


Continue Reading...
Write Article for Dev102

Write for Dev102!

We pay for user submitted tutorials and articles that we publish. Anyone can send in a contribution

Learn More