What is the maximum amount of characters that a typical path can contain for a directory when using C#?
使用C#时,典型路径可以包含的最大字符数是多少?
For example C:\test\
has 7 characters in length , what is the maximum length?
例如C:\ test \的长度为7个字符,最大长度是多少?
2 个解决方案
#1
33
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.
最大字符数由Win32 API库中的MAX_PATH定义。此设置为260,并且在CLR BCL内使用硬编码的相同设置。达到该字符数量的路径可能会带来麻烦(参见下文)。这个最大值是旧的FAT和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.
相反,默认情况下在大多数Windows安装中使用的NTFS文件系统最多具有32767个字符并支持unicode(在每个字符可占用2个字节的实现中,即UCS-2,而不是UTF-32)。但即使在NTFS中,单个路径段也不得超过255个字符。虽然NTFS支持非常长的文件名,但大多数应用程序(包括依赖于System.IO的任何.NET应用程序)都无法看到这些文件名。
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).
为什么260而不是256?因为驱动器说明符,第一个反斜杠和尾随空终止字符不是长度限制的一部分。您可以使用GetVolumeInformation获取Windows的此信息,您应该单独查询每个卷(每个卷可以具有不同的最大大小)。
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.
我假设Windows。 Linux和其他操作系统可能会有所不同。自Windows 10,build 1607以来,此限制已被删除,请参阅下面的详细信息。
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:
作为一般建议,您不应该依赖任何这些数字。相反,如果要通知用户路径太长,请捕获PathTooLongException:
try
{
SetCurrentDirectory(longPath);
}
catch(PathTooLongException exc)
{
Console.WriteLine("The pathname was 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).
注意:当您超过260个字符时,上面的代码将抛出,这是CLR对您施加的限制。这不是真正的限制(见第一段)。
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 is internal
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):
Microsoft已经确认,当前.NET的实现存在问题,您无法可靠地找出CLR支持的最大路径大小。如果要以编程方式获取此信息,请使用Path.MaxPath属性。但是,该属性是内部的,这意味着您只能通过反射访问它,并且您无法保证它可以跨版本或其他BCL实现(Mono)工作:
// reflection
FieldInfo maxPathField = typeof(Path).GetField("MaxPath",
BindingFlags.Static |
BindingFlags.GetField |
BindingFlags.NonPublic );
// invoke the field gettor, which returns 260
int MaxPathLength = (int) maxPathField.GetValue(null);
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).
注意:这为您提供了Microsoft .NET实现使用的最大路径。对于最大目录大小Path.MAX_DIRECTORY_PATH,BCL中有一个不同的值,但即使在BCL内部也不会使用它。如果您创建的目录等于此大小,则无法在该目录中放置任何文件。更糟糕的是,只是打开它会引发错误(因为强制的半目录别名。和..,这会导致许多API崩溃)。
UPDATE: as of Windows 10 Build 1607 you can remove the limit via OptIn in the Registry:
更新:从Windows 10 Build 1607开始,您可以通过注册表中的OptIn删除限制:
Starting in Windows 10, version 1607, MAX_PATH limitations have been removed from common Win32 file and directory functions. However, you must opt-in to the new behavior.
从Windows 10版本1607开始,MAX_PATH限制已从常见的Win32文件和目录函数中删除。但是,您必须选择加入新行为。
A registry key allows you to enable or disable the new long path behavior. To enable long path behavior set the registry key at
HKLM\SYSTEM\CurrentControlSet\Control\FileSystem LongPathsEnabled
(Type: REG_DWORD).注册表项允许您启用或禁用新的长路径行为。若要启用长路径行为,请在HKLM \ SYSTEM \ CurrentControlSet \ Control \ FileSystem LongPathsEnabled(类型:REG_DWORD)中设置注册表项。
More info is in the updated entry on MSDN, about halfway down.
更多信息在MSDN上的更新条目中,大约一半。
#2
-1
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.
如果使用\\?添加路径,则可以使UNC路径长于260。请参阅MSDN上的以下命名文件,路径和命名空间。
#1
33
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.
最大字符数由Win32 API库中的MAX_PATH定义。此设置为260,并且在CLR BCL内使用硬编码的相同设置。达到该字符数量的路径可能会带来麻烦(参见下文)。这个最大值是旧的FAT和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.
相反,默认情况下在大多数Windows安装中使用的NTFS文件系统最多具有32767个字符并支持unicode(在每个字符可占用2个字节的实现中,即UCS-2,而不是UTF-32)。但即使在NTFS中,单个路径段也不得超过255个字符。虽然NTFS支持非常长的文件名,但大多数应用程序(包括依赖于System.IO的任何.NET应用程序)都无法看到这些文件名。
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).
为什么260而不是256?因为驱动器说明符,第一个反斜杠和尾随空终止字符不是长度限制的一部分。您可以使用GetVolumeInformation获取Windows的此信息,您应该单独查询每个卷(每个卷可以具有不同的最大大小)。
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.
我假设Windows。 Linux和其他操作系统可能会有所不同。自Windows 10,build 1607以来,此限制已被删除,请参阅下面的详细信息。
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:
作为一般建议,您不应该依赖任何这些数字。相反,如果要通知用户路径太长,请捕获PathTooLongException:
try
{
SetCurrentDirectory(longPath);
}
catch(PathTooLongException exc)
{
Console.WriteLine("The pathname was 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).
注意:当您超过260个字符时,上面的代码将抛出,这是CLR对您施加的限制。这不是真正的限制(见第一段)。
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 is internal
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):
Microsoft已经确认,当前.NET的实现存在问题,您无法可靠地找出CLR支持的最大路径大小。如果要以编程方式获取此信息,请使用Path.MaxPath属性。但是,该属性是内部的,这意味着您只能通过反射访问它,并且您无法保证它可以跨版本或其他BCL实现(Mono)工作:
// reflection
FieldInfo maxPathField = typeof(Path).GetField("MaxPath",
BindingFlags.Static |
BindingFlags.GetField |
BindingFlags.NonPublic );
// invoke the field gettor, which returns 260
int MaxPathLength = (int) maxPathField.GetValue(null);
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).
注意:这为您提供了Microsoft .NET实现使用的最大路径。对于最大目录大小Path.MAX_DIRECTORY_PATH,BCL中有一个不同的值,但即使在BCL内部也不会使用它。如果您创建的目录等于此大小,则无法在该目录中放置任何文件。更糟糕的是,只是打开它会引发错误(因为强制的半目录别名。和..,这会导致许多API崩溃)。
UPDATE: as of Windows 10 Build 1607 you can remove the limit via OptIn in the Registry:
更新:从Windows 10 Build 1607开始,您可以通过注册表中的OptIn删除限制:
Starting in Windows 10, version 1607, MAX_PATH limitations have been removed from common Win32 file and directory functions. However, you must opt-in to the new behavior.
从Windows 10版本1607开始,MAX_PATH限制已从常见的Win32文件和目录函数中删除。但是,您必须选择加入新行为。
A registry key allows you to enable or disable the new long path behavior. To enable long path behavior set the registry key at
HKLM\SYSTEM\CurrentControlSet\Control\FileSystem LongPathsEnabled
(Type: REG_DWORD).注册表项允许您启用或禁用新的长路径行为。若要启用长路径行为,请在HKLM \ SYSTEM \ CurrentControlSet \ Control \ FileSystem LongPathsEnabled(类型:REG_DWORD)中设置注册表项。
More info is in the updated entry on MSDN, about halfway down.
更多信息在MSDN上的更新条目中,大约一半。
#2
-1
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.
如果使用\\?添加路径,则可以使UNC路径长于260。请参阅MSDN上的以下命名文件,路径和命名空间。