What is a good way to find the last file created in a folder in C #?


I have a network folder which can contain up to 10,000 files (usually around 5000).

What is the fatest way I can get the filepath of the most recently created file in that folder using c#?

Currently I am using the below, but wondered if there was a quicker way.


DirectoryInfo di = new DirectoryInfo(xmlFileLocation);
var feedFiles = di.GetFiles("*.xml");
var sortedFeedFile = from s in feedFiles
                     orderby s.CreationTime descending
                     select s;

if(sortedFeedFile.Count() > 0){
    mostRecentFile = sortedFeedFile.First();

Sorting the files is taking you O(nlogn) time. If all you need is the most recently created, it would be faster to just scan through the files and find the most recent---O(n).