如果DirectoryInfo.GetFiles()。长度超过Int32.MaxValue? [英] What if DirectoryInfo.GetFiles().Length exceeds Int32.MaxValue?

查看:166
本文介绍了如果DirectoryInfo.GetFiles()。长度超过Int32.MaxValue?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过另一个<一href="http://stackoverflow.com/questions/3766540/error-on-maximum-number-of-files/3767265#3767265">question关于文件的文件夹中的最大数,我注意到 这

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

 DirectoryInfo.GetFiles().Length

时,返回一个System.In32,但一个的Int32的最大值为

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

 2.147.483.647  (Int32.MaxValue) 

而在NTFS(在许多其它文件系统)的文件的最大数量可以远不止于此。

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

在NTFS是

 4.294.967.295 single files in one folder (probably an Uint32)

这使我一个有趣的问题:

Which leads me to the interesting question:

是否有可能得到的文件数量的文件夹中的NTFS与.NET Framework,当文件数超过Int32.MaxValue,在一个优雅和表演的方式?

注意:这不是为什么的问题。我知道,这些都是很多的文件;)

推荐答案

有一个 LongLength 属性阵列 ,它返回长度为。无论如何,如果的GetFiles 返回多 Int32.MaxValue 的项目,你将有问题,反正...就像一个 OutOfMemoryException异常

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

当你实际上并不需要的物品的数量,我建议你使用 EnumerateFiles 方法,而不是(在4.0中引入)。它不立即获取所有的文件名在内存中,而是获取它们一一

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

这篇关于如果DirectoryInfo.GetFiles()。长度超过Int32.MaxValue?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆