I am using VSTS 2008 + C# + .Net 3.5 to develop a console application. I need to enumerate the most recent 50 files in current folder (to read file content, to get file meta data like file name, creation time, etc.). Since the current folder has about 5,000 files, and if I use Directory.GetFiles API, all 5,000 files’ meta data information will be read into memory. I think it is a waste since I only need to access the most recent 50 files.
Any solutions to access only the 50 most recent files in current directory?
This solution still loads metadata about all files, but I would say it’s fast enough for most uses. The following code reports that it takes around 50ms to enumerate the 50 most recently updated files in my Windows\System32 directory (~2500 files). Unless the code is run very frequently I would probably not spend time optimizing it a lot more:
Update
Based on the discussion in the comments regarding using date/time in the file name: note that
Directory.GetFilesdoes not load metadata about files; it simply returns a string array with file names (DirectoryInfo.GetFileson the other hand returns an array ofFileInfoobjects). So, if you have date and time in your file names (preferably in a format that lends itself to sorting, such asyyyyMMdd-HHmmssor something like that) you can useDirectory.GetFilesto get the file names, sort descending and then pick the 50 first from the list:Then loop over the list and load whatever data you need from each file.