目录的最大字符数或长度是多少?
使用 C# 时,目录的典型路径可以包含的最大字符数是多少?
例如 C:\test\
的长度为 7 个字符,最大长度是多少?
What is the maximum amount of characters that a typical path can contain for a directory when using C#?
For example C:\test\
has 7 characters in length , what is the maximum length?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
CLR 中 MaxPath 的最大长度为 260 个字符。
最大字符数由 Win32 API 库中的 MAX_PATH 定义。此设置为 260,并且在 CLR BCL 内部使用硬编码的相同设置。达到该数量字符的路径可能会引起麻烦(见下文)。此最大值是旧式 FAT 和 FAT32 的最大值。
相反,默认情况下在大多数 Windows 安装上使用的 NTFS 文件系统最多有 32767 个字符并支持 unicode(在每个字符可以占用 2 个字节的实现中,即 UCS-2,而不是 UTF-32)。但即使在 NTFS 中,单个路径段也不得超过 255 个字符。虽然 NTFS 支持很长的文件名,但大多数应用程序(包括任何依赖于 System.IO 的 .NET 应用程序)将无法看到这些文件名。
为什么是 260 而不是 256?因为驱动器说明符、第一个反斜杠和结尾的空终止字符不是长度限制的一部分。您可以使用
获取 Windows 的此信息GetVolumeInformation
,您应该单独查询每个卷(每个卷可以有不同的最大大小)。我以为是Windows。 Linux 和其他操作系统可能会有所不同。自 Windows 10 build 1607 起,此限制已被删除,有关详细信息,请参阅下文。
作为一般建议,您不应依赖这些数字中的任何一个。相反,如果您想通知用户路径太长,请捕获 PathTooLongException:
注意:当您超过 260 个字符(这是 CLR 对您施加的限制)时,上面的代码将抛出异常。这不是真正的限制(参见第一段)。
的旁白
作为 .NET Microsoft 已确认这是 .NET 当前实现的一个问题,您无法可靠地找出 CLR 支持的最大路径大小。如果您想以编程方式获取此信息,请使用 Path.MaxPath 属性。但是,该属性是
internal
,这意味着您只能通过反射访问它,并且不能保证它可以跨版本或在其他 BCL 实现(Mono)上工作:注意:这为您提供了最大路径因为它被 Microsoft 的 .NET 实现所使用。 BCL 中的最大目录大小有一个不同的值 Path.MAX_DIRECTORY_PATH,但即使在 BCL 内部也从未使用过该值。如果您创建的目录等于此大小,您将无法在该目录中放置任何文件。更糟糕的是,仅仅打开它就会引发错误(因为强制的半目录别名
.
和..
,这会导致许多 API 崩溃)。更新:从 Windows 10 Build 1607 开始,您可以删除该限制通过注册表中的选择加入:
更多信息位于 更新了 MSDN 上标题为“最大路径长度限制”的部分。
Maximum for MaxPath in CLR is 260 characters
The maximum amount of characters is defined by
MAX_PATH
in the Win32 API library. This setting is 260 and that same setting is used, hard-coded, inside the CLR BCL. A path reaching that amount of characters is likely to cause trouble (see aside below). This maximum is the maximum for the good old FAT and FAT32.Conversely, the NTFS filesystem, used on the majority of Windows installations by default, has a maximum of 32767 characters and supports unicode (in an implementation where each character can take up 2 bytes, i.e., UCS-2, not UTF-32). But even in NTFS, a single path segment must not exceed 255 characters. While NTFS supports very long filenames, most applications, including any .NET application relying on
System.IO
, will not be able to see these filenames.Why 260 and not 256? Because the drive specifier, the first backslash and the trailing null-terminating character are not part of the length-limitations. You can get this information for Windows using
GetVolumeInformation
, which you should query for each volume individually (each volume can have a different max size).I assumed Windows. Linux and other OS's may and will be different. Since Windows 10, build 1607, this limit has been removed, see below for details.
As a general advice, you should not rely on any of these numbers. Instead, catch the PathTooLongException if you want to inform users that the path is too long:
Note: the code above will throw when you exceed 260 characters, which is the limit that the CLR is imposing on you. This is not the real limit (see first paragraph).
As an aside on .NET
Microsoft has confirmed that it is a problem with the current implementation(s) of .NET that you cannot reliably find out what the maximum path size is as supported by the CLR. If you want to get this information programmatically, use the
Path.MaxPath
property. However, the property isinternal
which means you can only access it through reflection and that you cannot guarantee it will work across versions, or on other BCL implementations (Mono):Note: this gives you the maximum path as it is used by Microsoft's .NET implementation. There's a different value in the BCL for the maximum directory size, Path.MAX_DIRECTORY_PATH, but even inside the BCL this is never used. If you ever create a directory equal to this size, you will not be able to place any files inside that directory. Worse, just opening it will raise an error (because of the mandatory semi-directory aliases
.
and..
, which causes many API's to crash).UPDATE: as of Windows 10 Build 1607 you can remove the limit via OptIn in the Registry:
More info is in the updated entry on MSDN, section titled 'Maximum Path Length Limitation'.
如果您在路径前添加
\\?
,则 UNC 路径的长度可以超过 260。请参阅 MSDN 上的以下命名文件、路径和命名空间。You can have UNC paths longer than 260 if you prepend the path with a
\\?
. See the following Naming Files, Paths and Namespaces on MSDN.