views:

48

answers:

1

How to Open a Directory Sorted by Date?

In C#, I can open a directory for reading each file. How to I make sure that the directory I investigate in my C# code is opened such that it is sorted by last modified date?

+2  A: 

You can use Linq to sort the files:

static void Main(string[] args)
{
    var files = Directory.GetFiles(@"d:\", "*").OrderByDescending(d => new FileInfo(d).LastWriteTime);
    foreach(var directory in files)
    {
        Console.WriteLine(directory);
    }

    Console.ReadLine();
}
TimothyP