Caspar Kleijne
Caspar Kleijne

Reputation: 21864

What if DirectoryInfo.GetFiles().Length exceeds Int32.MaxValue?

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 ;)

Upvotes: 8

Views: 1174

Answers (1)

Thomas Levesque
Thomas Levesque

Reputation: 292455

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

Upvotes: 6

Related Questions