Skip to content
Maarten Balliauw
← back
CSharp 1 min read

LINQ to filesystem

edit this page

The past few hours, I’ve been experimenting with LINQ. As a sample application, I’m trying to create a small photo album website, which shows me all images in a specific folder on my webserver.

What does LINQ have to do with that? Everyone has used a loop over all files in a folder, and I decided to try LINQ for that matter. Here’s how:

csharp
var rootFolder = "C:\\";
var selectedImages = from file in Directory.GetFiles(rootFolder, "*.jpg")
                             select new { Path = file,
                                          Name = new FileInfo(file).Name,
                                          CreationDate = new FileInfo(file).CreationTime,
                                          DirectoryName = new FileInfo(file).DirectoryName
                                    };

There you go! A collection named “selectedImages”, filled with anonymous class instances containg a file Path, Name, CreationDate and DirectoryName. This collection can now be bound to, for example, a GridView:

csharp
this.gridView1.DataSource = selectedImages;
this.gridView1.DataBind();

EDIT: (mental note to myself: add LINQ keywords to syntax highlighter…) - done!

Maarten Balliauw

Writing about .NET, web, performance and security, since 2006.

Related posts

★ Prefer this blog on Google