last-modified

How to find the most recent file in a directory using .NET, and without looping?

…衆ロ難τιáo~ 提交于 2019-11-26 01:49:11
问题 I need to find the most recently modified file in a directory. 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?. 回答1: 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(); 回答2: If

How to find the most recent file in a directory using .NET, and without looping?

試著忘記壹切 提交于 2019-11-26 00:51:53
I need to find the most recently modified file in a directory. 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(); If you want to search recursively, you can use this beautiful piece of code: public static FileInfo GetNewestFile