Logo

Maarten Balliauw {blog}

ASP.NET, ASP.NET MVC, Windows Azure, PHP, ...

About the author

Maarten Balliauw is currently employed as .NET Technical Consultant at RealDolmen. His interests are mainly web applications developed in ASP.NET (C#) or PHP and the Windows Azure cloud platform.
More about me More about me
Send mail E-mail me


ASP.NET MVC Quickly Pro NuGet Subscribe to my RSS feed Follow me on Twitter! View Maarten Balliauw's profile on LinkedIn
Maarten Balliauw - MVP - Most Valuable Professional
Maarten Balliauw - ASPInsider

Search

Latest Twitter

    Follow me on Twitter...

    Archive

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

    © Copyright Maarten Balliauw 2012


    LINQ to filesystem

    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:

    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:

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

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


    Categories: C# | General | LINQ | Software

    Comments (2) -

    Jos Belgium |

    Friday, July 11, 2008 8:51 PM

    Jos

    Could this code be extended to allow WHERE clauses (where name like '*.jpg' and ModifedDate > '1/1/2008'), or even aggregates (total filesize)? That would be even greater...

    Maybe it needs a dedicated Linq to FileSystem provider?

    Jos

    maartenba Belgium |

    Monday, July 14, 2008 7:52 AM

    maartenba

    I've seen such a thing somewhere, but can't remember where...

    Comments are closed