如果 DirectoryInfo.GetFiles().Length 超过 Int32.MaxValue 怎么办? [英] What if DirectoryInfo.GetFiles().Length exceeds Int32.MaxValue?

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

问题描述

关于最大文件数的另一个问题在一个文件夹中,我注意到那个

 DirectoryInfo.GetFiles().Length

正在返回 System.In32,但 Int32 的最大值是

 2.147.483.647 (Int32.MaxValue)

而在 NTFS(许多其他文件系统)上,文件的最大数量可能远远超出此范围.

在 NTFS 上是

 4.294.967.295 个文件夹中的单个文件(可能是 Uint32)

这让我想到了一个有趣的问题:

当文件数超过 Int32.MaxValue 时,是否可以使用 .NET 框架以优雅且高效的方式获取 NTFS 上文件夹中的文件数?>

注意:这不是为什么.我知道,这些文件很多;)

解决方案

Array 上有一个 LongLength 属性,它返回长度为 long.无论如何,如果 GetFiles 返回的项数超过 Int32.MaxValue 项,无论如何你都会遇到问题......就像 OutOfMemoryException ;)

当您实际上不需要项目数量时,我建议您改用 EnumerateFiles 方法(在 4.0 中引入).它不会一次获取内存中的所有文件名,而是一个一个地获取它们

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

解决方案

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

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

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