我有几次在不合时宜的时候遇到过这个问题:

尝试在具有深度路径的开源Java项目上工作 在源代码控制中存储深度fitness wiki树 试图使用Bazaar导入源代码控制树时出错

为什么会有这个限制?

为什么它还没有被移除?

您如何应对路径限制? 不,切换到Linux或Mac OS X并不是这个问题的有效答案;)


当前回答

从Windows 10。可以通过修改注册表项来消除限制。

Tip 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. 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). The key's value will be cached by the system (per process) after the first call to an affected Win32 file or directory function (list follows). The registry key will not be reloaded during the lifetime of the process. In order for all apps on the system to recognize the value of the key, a reboot might be required because some processes may have started before the key was set. The registry key can also be controlled via Group Policy at Computer Configuration > Administrative Templates > System > Filesystem > Enable NTFS long paths. You can also enable the new long path behavior per app via the manifest: <application xmlns="urn:schemas-microsoft-com:asm.v3"> <windowsSettings xmlns:ws2="http://schemas.microsoft.com/SMI/2016/WindowsSettings"> <ws2:longPathAware>true</ws2:longPathAware> </windowsSettings> </application>

其他回答

另一种处理方法是使用Cygwin,这取决于您想对文件做什么(即Cygwin命令是否适合您的需要)

例如,它允许复制、移动或重命名连Windows资源管理器都做不到的文件。当然,也可以处理它们的内容,如md5sum、grep、gzip等。

此外,对于你正在编写的程序,你可以将它们链接到Cygwin DLL,这将使它们能够使用长路径(我还没有测试这个)

它确实如此,而且出于某种原因,它是默认的,但你可以很容易地用这个注册表项覆盖它:

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem]
"LongPathsEnabled"=dword:00000001

参见:https://blogs.msdn.microsoft.com/jeremykuhne/2016/07/30/net-4-6-2-and-long-paths-on-windows-10/

引用这篇文章https://learn.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#maximum-path-length-limitation

Maximum Path Length Limitation In the Windows API (with some exceptions discussed in the following paragraphs), the maximum length for a path is MAX_PATH, which is defined as 260 characters. A local path is structured in the following order: drive letter, colon, backslash, name components separated by backslashes, and a terminating null character. For example, the maximum path on drive D is "D:\some 256-character path string<NUL>" where "<NUL>" represents the invisible terminating null character for the current system codepage. (The characters < > are used here for visual clarity and cannot be part of a valid path string.)

现在我们看到它是1+2+256+1或[drive][:\][path][null] = 260。从DOS时代开始,人们可以假设256是一个合理的固定字符串长度。回到DOS api,我们意识到系统跟踪每个驱动器的当前路径,我们有26个(32个带符号)最大驱动器(和当前目录)。

INT 0x21 AH=0x47表示“该函数返回不带驱动器号和初始反斜杠的路径描述。”因此,我们看到系统将CWD存储为一对(驱动器,路径),您通过指定驱动器(1= a, 2=B,…)来请求路径,如果您指定0,那么它假设由INT 0x21 AH=0x15 AL=0x19返回的驱动器的路径。现在我们知道为什么是260而不是256了,因为这4个字节没有存储在路径字符串中。

为什么是256字节的路径字符串,因为640K的内存足够了。

这并不是严格意义上的,因为NTFS文件系统支持最多32k个字符的路径。你可以使用win32 api和“\\?”使用大于260个字符作为路径前缀。

. net BCL团队博客中关于漫长路径的详细解释。 一个小节选强调了长路径的问题

Another concern is inconsistent behavior that would result by exposing long path support. Long paths with the \\?\ prefix can be used in most of the file-related Windows APIs, but not all Windows APIs. For example, LoadLibrary, which maps a module into the address of the calling process, fails if the file name is longer than MAX_PATH. So this means MoveFile will let you move a DLL to a location such that its path is longer than 260 characters, but when you try to load the DLL, it would fail. There are similar examples throughout the Windows APIs; some workarounds exist, but they are on a case-by-case basis.

从Windows 10。可以通过修改注册表项来消除限制。

Tip 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. 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). The key's value will be cached by the system (per process) after the first call to an affected Win32 file or directory function (list follows). The registry key will not be reloaded during the lifetime of the process. In order for all apps on the system to recognize the value of the key, a reboot might be required because some processes may have started before the key was set. The registry key can also be controlled via Group Policy at Computer Configuration > Administrative Templates > System > Filesystem > Enable NTFS long paths. You can also enable the new long path behavior per app via the manifest: <application xmlns="urn:schemas-microsoft-com:asm.v3"> <windowsSettings xmlns:ws2="http://schemas.microsoft.com/SMI/2016/WindowsSettings"> <ws2:longPathAware>true</ws2:longPathAware> </windowsSettings> </application>