.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> LINQ >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

File System Search Query in LINQ

Posted By:Sajid P K       Posted Date: November 26, 2010    Points: 15    Category: LINQ    URL: http://www.dotnetspark.com  

In this Code snippiest, I will show you query for searching a file in a file system using LINQ
 

Here I going to show you, how to use LINQ to find out /Search files in a Directory depending on a filtering condition.   This will search only in current Directory.

If want you can give a try to extend this search to inner directories as well

Query syntax

System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("Serach Folder Path here");

var Result = from  file in dir.GetFiles()  
             where file.Name.Contains( "Filter string Here "  )     
             orderby file.Extension           
             select new { file.Name, file.Extension, file.Length, file.FullName };

Lambda expression

System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("Serach Folder Path here");


var Result = dir.GetFiles().Where(x => x.Name.ToLower().Contains("Filter string Here " )).OrderBy(x => x.Extension)       .Select(x => new { x.Name, x.Extension, x.Length, x.FullName });

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend