如何找到使用.NET的目录中最新的文件,而不用循环? [英] How to find the most recent file in a directory using .NET, and without looping?

查看:193
本文介绍了如何找到使用.NET的目录中最新的文件,而不用循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到一个目录中的最新修改的文​​件。

I need to find the most recently modified file in a directory.

我知道我可以遍历文件夹中的所有文件,并比较 File.GetLastWriteTime ,但有一个更好的方式来做到这一点不用循环?

I know I can loop through every file in a folder and compare File.GetLastWriteTime, but is there a better way to do this without looping?.

推荐答案

如何对这样的事情...

how about something like this...

var directory = new DirectoryInfo("C:\\MyDirectory");
var myFile = (from f in directory.GetFiles()
             orderby f.LastWriteTime descending
             select f).First();

// or...
var myFile = directory.GetFiles()
             .OrderByDescending(f => f.LastWriteTime)
             .First();

这篇关于如何找到使用.NET的目录中最新的文件,而不用循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆