Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DirectoryInfo GetFiles TOP number

Tags:

c#

.net

fileinfo

I want to return 10 files only from a directory. Is this possible?

DirectoryInfo d = new DirectoryInfo(HttpContext.Current.Server.MapPath("~/xml"));

FileInfo[] files = d.GetFiles("*.xml");

This way returns all XML files, but I want to get just the first ten.

like image 837
Beginner Avatar asked May 05 '11 14:05

Beginner


1 Answers

You can add the extension method Take(10) to only grab the first 10 files.

var d = new DirectoryInfo(HttpContext.Current.Server.MapPath("~/xml"));
var files = d.GetFiles("*.xml").OrderByDescending(fi=>fi.LastWriteTime).Take(10);
like image 122
Jake Pearson Avatar answered Sep 29 '22 20:09

Jake Pearson