I have a directory with around 15-30 thousand files. I need to just pull the oldest one. In other words the one that was created first. Is there a quick way to do this using C#, other than loading them into a collection then sorting?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Generic Generics in Managed C++
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
Look, would it not be easier to shell out to a hidden process and redirect the output stream to the input and use the
dir /o-d
which sorts by the date/time, using the dash reverses the operation....Edit: here's a sample code to do this...quick and dirty...
The very first 4 or 5 lines of the StringBuilder object
sbRedirectedOutput
can be chopped out,then after that line would contain the oldest filename and would be quite easy to parse out....Sorting is
O(n log n)
. Instead, why don't you just enumerate the directory? I'm not sure what the C# equivalent ofFindFirstFile()
/FindNextFile()
is, but you want to do is:Keep the current lowest date and filename in a local variable.
Enumerate the directory.
Here's a C# routine that may do what you want by spawning a cmd shell execute a
dir /o:D
on the specified directory and returning the name of the first file found.