Daniel Lip
Daniel Lip

Reputation: 11319

How to get the first created folder instead the last?

var lastWrittenFolder = new DirectoryInfo(textBoxPath.Text).GetDirectories()
                       .OrderByDescending(d => d.LastWriteTimeUtc).First();

this is working fine for getting the latest created folder.

but how do i get the first created folder ?

Upvotes: 0

Views: 61

Answers (4)

Eliano
Eliano

Reputation: 33

You are already there. Just instead of sorting the folder in reverse order ( bottom to top) just sort them from top to bottom and then select the first one like you did.

Here is correct code :

var lastWrittenFolder = new DirectoryInfo(path).GetDirectories().OrderBy(d => d.LastWriteTimeUtc).First();

Upvotes: 0

Vivek Nuna
Vivek Nuna

Reputation: 1

You can also use Min, so you need the record which has the minimum creation date.

var firstCreatedFolder = new DirectoryInfo(textBoxPath.Text).GetDirectories()
                       .Min(d => d.CreationTimeUtc);

Upvotes: 0

Daya
Daya

Reputation: 9

following code will help you.

static void Main()
{
    var folderPath = "your-folder-path";

    var directories = new DirectoryInfo(folderPath).GetDirectories();

    foreach (var item in directories.OrderBy(m => m.LastWriteTime))
    {
        Console.WriteLine(item.LastWriteTime + " " + item.Name);
    }


    Console.ReadLine();
}

Upvotes: 0

Benny Shtemer
Benny Shtemer

Reputation: 56

Change the OrderBy function, and the keySelector parameter:

var lastWrittenFolder = new DirectoryInfo(textBoxPath.Text).GetDirectories()
                   .OrderBy(d => d.CreationTimeUtc).First();

Upvotes: 3

Related Questions