开发者

DirectoryInfo GetFiles TOP number

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");
开发者_StackOverflow

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


If you're using .NET4 then you should probably use EnumerateFiles instead, along with the Take extension method:

var d = new DirectoryInfo(HttpContext.Current.Server.MapPath("~/xml"));
FileInfo[] files = d.EnumerateFiles("*.xml").Take(10).ToArray();


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);


you have to the same what Jake mentioned, but not FileInfo[] files

      DirectoryInfo d = new DirectoryInfo("~/xml");
      IEnumerable< FileInfo> files = d.GetFiles().Take(10);

                         OR

     DirectoryInfo d = new DirectoryInfo("~/xml");
      FileInfo[] files = d.GetFiles().Take(10).ToArray();


  var directory = new DirectoryInfo(Tab16_mainPath);


  var myFile = (from f in directory.GetFiles().Take(3)
                orderby f.LastWriteTime descending
                select f).ToArray();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜