Using C# (.NET 2.0), what is the best way to retrieve a file listing in a network directory, with preference to either sort by last write time, or to exclude the return based on last write time?
I’m currently bringing back a listing using the GetFiles method of a DirectoryInfo instance. The directories I encounter can contain over 6,000 files and just to get an array of FileInfo back takes almost 25 seconds.
WMI will not work and I cannot see anything else that is .NET-centric that will help in this situation. Am I missing something? Is there a better solution than to use GetFiles?
Thank you.
Update
I just noticed this old answer, for .net 4.0 and later there is a
System.IO.EnumerateFilesmethod that does this for you, for everything pre .net 4.0, read on..Net 3.5 & Earlier
I’m with Stefan, I encountered this exact problem, and rolled my own enumerator to iterate over folders with +100k files.
This class wraps all the API specific stuff you’ll need to use FindFirstFile and FindNextFile.
Hope this helps,