首页 > 解决方案 > 如何使用 '\\?\' 前缀处理长的相对路径?

问题描述

所以我们都知道,Windows 程序默认限制处理最大路径长度为 260 个字符。\\?\但是,可以通过在路径前加上字符序列来轻松克服此限制。

然而,由于某种原因,这对于相对路径来说是不可能的,正如 MSDN 所说:

因为您不能将\\?\前缀与相对路径一起使用,所以相对路径始终限于MAX_PATH字符总数。

来源

我真的不明白为什么微软决定禁止以相对路径为前缀,\\?\所以如果这个决定背后有某种理由,我会很高兴听到它,因为它真的没有意义我\\?\只允许完整路径。

不过,我真正的问题是如何处理这个限制:我是否应该简单地调用GetFullPathName()相对路径以将它们扩展到完整路径,然后添加\\?\前缀,然后将该路径传递给fopen()etc.,或者推荐的处理方式是什么有这个限制?

标签: winapi

解决方案


You cannot use the \\?\ prefix with a relative path.

When relative path is passed to the system, it is parsed as absolute paths and then passed to the system. And as it is mentioned in the source:

The prefixes \\:\ are not used as part of the path itself. They indicate that the path should be passed to the system with minimal modification, which means that you cannot use forward slashes to represent path separators, or a period to represent the current directory, or double dots to represent the parent directory.


推荐阅读