c# - 如何在 .Net 中获得最大允许文件大小?

标签 c# .net filesize

有谁知道如何(本地)获取给定驱动器/文件夹/目录的最大允许文件大小?至于 Fat16,它是 ~2gb,据我记得,Fat32 是 4gb,对于较新的 NTFS 版本,它远远超出了这个范围......更不用说 Mono 和底层操作系统了。

有什么我可以读出/检索的东西可以给我一些提示吗?基本上我 - 知道 - 应用程序可能会产生比 2gb 更大的单个文件,我想在用户设置相应的输出路径时检查它...

干杯,谢谢, -J

最佳答案

这可能不是理想的解决方案,但无论如何我都会提出以下建议:

// Returns the maximum file size in bytes on the filesystem type of the specified drive.
long GetMaximumFileSize(string drive)
{
    var driveInfo = new System.IO.DriveInfo(drive)

    switch(driveInfo.DriveFormat)
    {
        case "FAT16":
            return 1000; // replace with actual limit
        case "FAT32":
            return 1000; // replace with actual limit
        case "NTFS":
            return 1000; // replace with actual limit
    }
}

// Examples:
var maxFileSize1 = GetMaximumFileSize("C"); // for the C drive
var maxFileSize2 = GetMaximumFileSize(absolutePath.Substring(0, 1)); // for whichever drive the given absolute path refers to

This维基百科上的页面包含各种文件系统的最大文件大小的非常全面的列表。根据要在 GetMaximumFileSize 函数中检查的文件系统的数量,您可能想要使用 Dictionary 对象甚至简单的数据文件而不是 switch 语句。

现在,您可能直接使用 WMI 或什至 Windows API 检索最大文件大小,但这些解决方案当然只与 Windows 兼容(即与 Mono/Linux 不兼容) .然而,我认为这是一个相当不错的纯托管解决方案,尽管使用了查找表,并且具有在所有操作系统上可靠工作的好处。

希望对您有所帮助。

关于c# - 如何在 .Net 中获得最大允许文件大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/668380/

相关文章:

linux - 在 linux 中分配文件而不清零且不创建稀疏文件

c# - 支持 Ajax 的 WCF 服务 - 实现观察者设计模式

c# - 关于动态文本框的一般概念

python - 从 Python 中的压缩文本文件中读取行并获取读取的原始压缩字节数

c# - LINQ(无法将 'System.Double' 类型的对象转换为 'System.String' 类型。)

c# - 使用 Aspose.PDF 在 PDF 中嵌入 TTF 字体

c++ - 优化 : Shrinking file size in C or C++

c# - 如何使用 SevenZipSharp 将文件添加到存档

c# - 有没有办法在 Windows 10 中使用蓝牙 LE 库 C# 与传感器或微 Controller 进行通信?

c# - 报表查看器中的水平表