windows 如何在路径长度大于 256 的窗口中创建目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6996711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create directories in windows with path length greater than 256
提问by Muthukumar Palaniappan
I have several level of directories in the folder path. when the path exceeds 256, then I could not create a sub-folder or file from it. Is there any chance to build paths more than this length. Can anyone help me out.
我在文件夹路径中有几个级别的目录。当路径超过 256 时,我无法从中创建子文件夹或文件。有没有机会建立超过这个长度的路径。谁能帮我吗。
采纳答案by David Heffernan
In fact the limit on path strings is 260 characters. The underlying OS, these days, can support much longer path names, up to 32,767 characters. In order to name a path with a long name you need to use the magic \\?\
prefix, and use the Unicode version of the API.
事实上,路径字符串的限制是 260 个字符。如今,底层操作系统可以支持更长的路径名,最多 32,767 个字符。为了使用长名称命名路径,您需要使用魔法\\?\
前缀,并使用 API 的 Unicode 版本。
However, many tools don't support such long names. A classic example of such a tool is Explorer which won't let you create objects with names longer than 260 characters. Because of this I strongly advise you to avoid creating such long names—doing so will save you much heartache in the long run.
但是,许多工具不支持这么长的名称。此类工具的一个经典示例是 Explorer,它不允许您创建名称超过 260 个字符的对象。因此,我强烈建议您避免创建如此长的名称——从长远来看,这样做会为您省去很多心痛。
回答by JAB
This should get you started: http://msdn.microsoft.com/en-us/library/aa365247%28v=vs.85%29.aspx#maxpath
这应该让你开始:http: //msdn.microsoft.com/en-us/library/aa365247%28v=vs.85%29.aspx#maxpath
Sadly it's an issue that I don't think will be going away any time soon, so you'd do well to familiarize yourself with that stuff.
遗憾的是,我认为这是一个短期内不会消失的问题,因此您最好熟悉一下这些内容。
As an aside, if you have access to robocopy
(comes packaged with Windows Vista and 7, but is also available for XP), which supports long paths, you could create your files/subfolders in a higher-up folder and then use robocopy to move the subfolder to its desired location deeper in the folder tree.
顺便说一句,如果您可以访问robocopy
(随 Windows Vista 和 7 一起打包,但也可用于 XP),它支持长路径,您可以在更高级别的文件夹中创建您的文件/子文件夹,然后使用 robocopy 移动将子文件夹移动到文件夹树中更深处的所需位置。
回答by Kratz
According to the documentation here http://msdn.microsoft.com/en-us/library/Aa365247, the maximum length is actually about 32,000, but most windows APIs still limit you to MAX_PATH
which is 260. There are some unicode APIs that let you go beyond the 260 limit.
根据此处的文档http://msdn.microsoft.com/en-us/library/Aa365247,最大长度实际上约为 32,000,但大多数 Windows API 仍将您限制MAX_PATH
为 260。有一些 unicode API 可以让你超出了 260 的限制。
See here, http://msdn.microsoft.com/en-us/library/aa363856.
请参阅此处,http://msdn.microsoft.com/en-us/library/aa363856。
In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend
\\?\
to the path. For more information, see Naming a File.
在此函数的 ANSI 版本中,名称限制为 MAX_PATH 个字符。要将此限制扩展到 32,767 个宽字符,请调用该函数的 Unicode 版本并
\\?\
在路径前加上前缀。有关更多信息,请参阅命名文件。
回答by Migrate2Lazarus see my profile
This is an addendum to the answeres above. I extracted only a summary to what I thinkis relevant, from Microsoft's official documentation:
这是对上述答案的补充。我仅从 Microsoft 的官方文档中提取了我认为相关的摘要:
Maximum Path Length Limitation
In the Windows API (with some exceptions), 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.
Example: "D:\some 256-character-path-string" -> 256
最大路径长度限制
在 Windows API 中(有一些例外),路径的最大长度是 MAX_PATH,定义为 260 个字符。本地路径按以下顺序构造:驱动器号、冒号、反斜杠、由反斜杠分隔的名称组件和终止空字符。
示例:"D:\some 256-character-path-string" -> 256
Using long paths
The Windows API has many functions that also have Unicode versions to permit an extended-length path for a maximum total path length of 32,767 characters.
To specify an extended-length path, use the "\?\" prefix. For example, "\?\D:\very long path".
使用长路径
Windows API 有许多函数也有 Unicode 版本,以允许最大总路径长度为 32,767 个字符的扩展长度路径。
要指定扩展长度的路径,请使用“\?\”前缀。例如,“\?\D:\非常长的路径”。
Relative paths
Relative paths are always limited to a total of MAX_PATH characters.
相对路径
相对路径总是限制为总共 MAX_PATH 个字符。
Enable Long Paths in Win10
Starting in Windows 10.1607, MAX_PATH limitations have been removed from common Win32 file and directory functions.
However, you must opt-in to the new behavior.
在 Win10 中启用长路径
从 Windows 10.1607 开始,MAX_PATH 限制已从常见的 Win32 文件和目录功能中删除。
但是,您必须选择加入新行为。
From Microsoft documentation:
https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file?redirectedfrom=MSDN#maximum-path-length-limitation
来自 Microsoft 文档:https:
//docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file?redirectedfrom=MSDN#maximum-path-length-limitation
Warning for Delphi users:
There is a problem in IOUtils. It cannot be used in conjunction with Max_Path. It uses InternalCheckDirPathParam all over the place!
Delphi 用户警告:
IOUtils 中存在问题。它不能与 Max_Path 结合使用。它到处使用 InternalCheckDirPathParam !
Details: TDirectory.GetDirectoryRoot does not handle correctly paths of Max_Path characters