什么是字符或目录的最大金额的长度? [英] What is the maximum amount of characters or length for a Directory?

查看:278
本文介绍了什么是字符或目录的最大金额的长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是字符的一个典型路径可以使用C#时,包含一个目录的最高金额?

What is the maximum amount of characters that a typical path can contain for a directory when using C#?

例如 C:\?测试\ 在长度为7个字符,什么是最大长度

For example C:\test\ has 7 characters in length , what is the maximum length?

推荐答案

字符的最大数量由 MAX _ 在Win32 API库中定义。这个设置是260,而同样的设置时,硬codeD,在CLR BCL里面。达到字符的数量的路径可能会造成麻烦(见一旁下文)。这是最大最大的好旧FAT和FAT32。

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.

相反,在NTFS文件系统,对大多数Windows安装默认的使用,有一个最大的32767个字符,并支持单向code(在一个实施,其中每个字符会占用2个字节,即,UCS-2 ,不是UTF-32)。但是,即使在NTFS中,一个单一的路径段不得超过255个字符。虽然NTFS可以支持非常长文件名,大多数应用程序,包括任何.NET应用程序依赖于 System.IO ,将无法看到这些文件名。

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.

为什么260,而不是256?由于盘符,第一个反斜杠和尾随空终止符不长,限制部分。你可以得到这个信息的Windows使用<一个href="http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx#maxpath"><$c$c>GetVolumeInformation,你应该单独查询每卷(每卷可以有不同的最大尺寸)。

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

我认为Windows操作系统。 Linux和其他操作系统的可能,将是不同的。

I assumed Windows. Linux and other OS's may and will be different.

作为一般的建议,你不应该依赖任何这些号码。相反,赶PathTooLongException如果你想告诉用户该路径太长:

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:

try
{
    SetCurrentDirectory(longPath);
}
catch(PathTooLongException exc)
{
    Console.WriteLine("The pathname was too long");
}

注:code以上时,超过260个字符,这就是CLR是强加给你的限制会抛出。这不是真正的限制(见第一段)。

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

微软<一href="http://connect.microsoft.com/VisualStudio/feedback/details/94094/system-environment-should-have-a-maxpath-property">has证实,这是问题与当前执行的.NET(S),你不能可靠地找出最大路径大小是由CLR的支持。如果你想以编程方式获取此信息,请使用 Path.Max​​Path 属性。但是,属性是内部这意味着你只能通过反射来访问它,你不能保证它会跨版本工作,或在其他BCL实现(单声道):

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

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

请注意:这给你最大的路径,因为它是使用微软的.NET实现。有一个在首创置业的最大目录大小,Path.MAX_DIRECTORY_PATH不同的值,但即使里面的首创置业,这是从来没有使用过。如果你创建一个目录等于这个尺寸,你将不能够把这个目录中的所有文件。更糟的是,刚刚打开它会提高,因为强制半目录别名错误( .. ,其中导致许多API的崩溃)。

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

这篇关于什么是字符或目录的最大金额的长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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