views:

117

answers:

1

By another question about the maximum number of files in a folder, I noticed that

 DirectoryInfo.GetFiles().Length

is returning a System.In32, but the Maximum value of a Int32 is

 2.147.483.647  (Int32.MaxValue) 

while on NTFS (an many other filesystems) the maximum number of files can go far beyond that.

on NTFS it is

 4.294.967.295 single files in one folder (probably an Uint32)

Which leads me to the interesting question:

Is it possible to get the number of files in a folder on NTFS with the .NET framework, when the number of files exceeds the Int32.MaxValue, in an elegant and performing manner?

note: this is not a matter of why. and I know, those are a lot of files ;)

+4  A: 

There is a LongLength property on Array, which returns the length as a long. Anyway, if GetFiles returns more than Int32.MaxValue items, you will have problems anyway... like an OutOfMemoryException ;)

When you don't actually need the number of items, I suggest you use the EnumerateFiles method instead (introduced in 4.0). It doesn't fetch all the filenames in memory at once, instead it fetches them one by one

Thomas Levesque
so I have to loop trough sets of In32.Max (or less) and add results of DirectoryInfo.EnumerateFiles().Count(), like in a nice and gentle manner?
Caspar Kleijne
If you only want the number of files (not the filenames themselves), yes, EnumerateFiles will probably be more efficient (and will use much less memory)
Thomas Levesque