Derik Whittaker

Syndication

News


Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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,


Posted 03-30-2008 4:31 PM by Derik Whittaker
Filed under:

[Advertisement]

Comments

Anas Ghanem wrote re: Using Linq to access the file system
on 03-30-2008 7:23 PM

Great ,

Thanks

Kalpesh wrote re: Using Linq to access the file system
on 03-31-2008 3:43 AM

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.

Dew Drop - March 31, 2008 | Alvin Ashcraft's Morning Dew wrote Dew Drop - March 31, 2008 | Alvin Ashcraft's Morning Dew
on 03-31-2008 8:05 AM

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

DotNetKicks.com wrote Using Linq to access the file system
on 04-02-2008 8:32 AM

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

Derik Whittaker wrote re: Using Linq to access the file system
on 04-02-2008 8:36 AM

@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.

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)