Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 457
Caspar Kleijne Avatar asked Sep 22 '10 12:09

Caspar Kleijne


1 Answers

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

like image 147
Thomas Levesque Avatar answered Nov 15 '22 12:11

Thomas Levesque