using System.Linq;
DirectoryInfo info = new DirectoryInfo("PATH_TO_DIRECTORY_HERE");
FileInfo[] files = info.GetFiles().OrderBy(p => p.CreationTime).ToArray();
foreach (FileInfo file in files)
{
// DO Something...
}
@jing: "The DirectoryInfo solution is much faster then this (especially for network path)"
I cant confirm this.
It seems as if Directory.GetFiles triggers a filesystem or network cache.
The first request takes a while, but the following requests are much faster, even if new files were added.
In my test I did a Directory.getfiles and a info.GetFiles with the same patterns and both run equally
GetFiles done 437834 in00:00:20.4812480
process files done 437834 in00:00:00.9300573
GetFiles by Dirinfo(2) done 437834 in00:00:20.7412646
If the performance is an issue, you can use this command in MS_DOS:
dir /OD >d:\dir.txt
This command generate a dir.txt file in **d:** root the have all files sorted by date.
And then read the file from your code.
Also, you add other filters by * and ?.