Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Derik Whittaker

Thoughts on Software Development, .Net, OOP, Design Patterns and all things cool



Using Linq to access the file system

Again, this post is another in the recent string of How-to's.  But it is just so hard to resist.  .Net 3.5 has so many cool features, it is just fun to explore them all.

In today's post I thought it would be fun to use Linq to access the file system.  I thought I would show how to query both for folders and for files.  In each example I will give the non-Linq way and the Linq way.

Searching for a giving Directory (by name)

Non-Linq way

FileInfo[] files = new DirectoryInfo( @"K:\SomeFolder" ).GetFiles();

foreach ( FileInfo file in files )
{
        if ( file.Name == "SomeFileName.txt" )
	{
        	have a match, do something now
	}
}

Linq way

var files = from file in new DirectoryInfo(@"K:\SomeFolder").GetFiles()
	where file.Name == "SomeFileName.txt"
        select file;

Searching for a given File (by name)

Non-Linq way

DirectoryInfo[] di = new DirectoryInfo( @"K:\SomeFolder" ).GetDirectories();

foreach ( DirectoryInfo directoryInfo in di )
{
        if ( directoryInfo.Name == "SubFolderName")
	{
		// have a match, do something now
	}
}

Linq way

var folders = from folder in new DirectoryInfo(@"K:\SomeFolder").GetDirectories()
	where folder.Name == "SubFolderName"
        select folder;

The above Linq code is not better or worse really, just different.  And like I said before, if you are going to be using .Net 3.5, might as well use the new features.

Till next time,


Published Mar 30 2008, 04:31 PM by Derik Whittaker
Filed under:

Comments

Anas Ghanem said:

Great ,

Thanks

# March 30, 2008 7:23 PM

Kalpesh said:

Not to be picky - I assume you are showing a way to use linq & its a demo of linq.

Other than that - you could use Directory.Exists("full path") instead of looping through subdirectories and File.Exists to check for existence. Later, using Get to get hold of that dir/file.

# March 31, 2008 3:43 AM

Dew Drop - March 31, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - March 31, 2008 | Alvin Ashcraft's Morning Dew

# March 31, 2008 8:05 AM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# April 2, 2008 8:32 AM

Derik Whittaker said:

@Kalpesh,

Yes i am showing how to use linq to access this information.  And yes you can do it your way.  However, with linq i can do things such as apply better filters, do ordering, apply selection filters, etc.

# April 2, 2008 8:36 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Derik Whittaker

Derik is a .Net Developer/Architect specializing in WinForms working out the northern suburbs of Chicago. He is also believer and advocate for Agile development including SCRUM, TDD, CI, etc.

When Derik is not writing code he can be found spending time with his wife and young son, climbing on his bouldering wall, watching sports (mostly baseball), and generally vegging out. Check out Devlicio.us!

Our Sponsors

Proudly Partnered With


This Blog

Syndication

News