Reputation: 2520
I've got a problem counting the files on a fileshare (which are present longer than 1 hour):
The two 'known' methods:
GetFiles
dir.Getfiles.Where(Function(x) x.CreationTime < DateTime.Now.AddHours(-1)).Count()
Link: GetFiles
EnumerateFiles
dir.EnumerateFiles.Where(Function(x) x.CreationTime < DateTime.Now.AddHours(-1)).Count()
Link: EnumerateFiles
Is there any faster method to count the number of files inside a folder/fileshare?
The number of files may vary from 2000 to upwards of 500 000. Both methods shown above show a drastic drop in performance beyond the 30 000 files.
Questions found on SO that didn't resolve it for me:
fastest-way-to-count-folder-files-in-net-4-0
how-to-correctly-count-the-number-of-files-in-a-folder
Upvotes: 1
Views: 1622
Reputation: 45096
Another option is:
Track add and delete.
Add and remove from a collection.
I would assume DirectoryInfo.EnumerateFiles would be faster.
For two reasons:
One it enumerates before the whole collection returns
Two it includes date. Getfiles only returns the name so it may be making a second trip to get the date.
Upvotes: 1