List all Files in .NET 4.0 Based on the Creation Date

I had recently posted on 7 New methods to Enumerate Directory and Files in .NET 4.0

A user commented asking if it was possible to returns a list of files from a directory for a given date range (i.e. start date - end date). Here’s how to do so:


using System;
using System.Linq;
using System.IO;

namespace ConsoleApplication1
class Program
static void Main(string[] args)
DirectoryInfo DirInfo = new DirectoryInfo(@"D:\Articles\Pics\jQuery");

DateTime dt1 = new DateTime(2009, 07, 15);
DateTime dt2 = new DateTime(2010, 04, 15);

// LINQ query for files between 15-July 2009 and 15-April 2010.
var files = from file in DirInfo.EnumerateFiles()
where file.CreationTimeUtc > dt1 &
file.CreationTimeUtc < dt2                       
select file;

// Show results.
foreach (var file in files)
Console.WriteLine("{0} created on {1}", file.Name, file.CreationTimeUtc);

VB.NET 10.0 (converted)

Namespace ConsoleApplication1
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim DirInfo As New DirectoryInfo("D:\Articles\Pics\jQuery")

Dim dt1 As New Date(2009, 07, 15)
Dim dt2 As New Date(2010, 04, 15)

' LINQ query for files between 15-July 2009 and 15-April 2010.
Dim files = From file In DirInfo.EnumerateFiles()
Where file.CreationTimeUtc > dt1 And file.CreationTimeUtc < dt2
Select file

' Show results.
For Each file In files
Console.WriteLine("{0} created on {1}", file.Name, file.CreationTimeUtc)
Next file
End Sub
End Class
End Namespace



Will you give this article a +1 ? Thanks in advance

About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

No comments: