Search code examples
c#winformslinqdirectoryinfo

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 ?


Solution

  • Change the OrderBy function, and the keySelector parameter:

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