ErocM
ErocM

Reputation: 4662

get latest 5 files in directory listing by date

I have this for getting the files in order by date but how do I get the most recent 5 files in a list regardless if there is not 5:

    var files = Directory.GetFiles(Settings.SharedDirectory, "*.log").OrderByDescending(d => new FileInfo(d).LastWriteTime);
    foreach (var directory in files)
    {
      Console.WriteLine(directory);
    }

I know how to get the .Last but I need the last 5.

Thanks!

Upvotes: 3

Views: 214

Answers (1)

SLaks
SLaks

Reputation: 887469

You want .Take(5).​​​​​​​​​​​​​​

Upvotes: 4

Related Questions