SetCurrentDirectoryW中的错误206

时间:2017-06-13 06:11:44

标签: windows winapi visual-c++ visual-studio-2015

previous unclear question之后,我以某种方式创建了一个具有长路径名的目录。但是,如果我尝试通过添加长路径名前缀来访问它,它仍会抛出错误,如下所示。

ERROR_FILENAME_EXCED_RANGE
206 (0xCE)
The filename or extension is too long.

以下是我正在使用的代码段(在Windows 7上使用VS 2015 Update 3编译器)

#include <iostream>
#include <windows.h>

int main()
{
    const std::wstring wdir_path (L"\\\\?\\c:\\temp\\aLongPathnameComponent\\aLongPathNameComponent\\aLongPathNameComponent\\aLongPathNameComponent\\aLongPathNameComponent\\aLongPathNameComponent\\aLongPathNameComponent\\aLongPathNameComponent\\aLongPathNameComponent\\aLongPathNameComponent\\aLongPathNameComponent\\aLongPathNameComponent\\aLongPathNameComponent\\aLongPathNameComponent\\aLongPathNameComponent\\aLongPathNameComponent\\aLongPathNameComponent");

    if (!SetCurrentDirectoryW(wdir_path.c_str()))
    {
        printf("SetCurrentDirectory failed (%d)\n", GetLastError());
    }

    return 0;   
}

接下来,我尝试在{10}中提到的版本大于1607的Windows 10上运行它,我设置了注册表项并重建了上面的代码但是当我运行它时,我仍然得到相同的错误。我已多次阅读文档,我不确定我在这里做错了什么。有人可以指出我正确的方向吗?

更新

1)这是我用于Windows 7的清单文件

<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<assembly xmlns='urn:schemas-microsoft-com:asm.v1' manifestVersion='1.0'>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel level='asInvoker' uiAccess='false' />
      </requestedPrivileges>
    </security>
  </trustInfo>
</assembly>
<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>

我还在注册表项中添加了一个新的DWORD值,如2)所述。但是,在Windows 7上,它只是不起作用。

2)在Windows 10上,注册表项方法有效。我按照文档

修改了以下注册表项
HKLM\SYSTEM\CurrentControlSet\Control\FileSystem LongPathsEnabled (Type: REG_DWORD)

如果我禁用此功能,则会失败,反之亦然。

2 个答案:

答案 0 :(得分:3)

此功能的MSDN文档非常糟糕。将路径作为输入的大多数函数将允许您通过在路径前加MAX_PATH来绕过L"\\?\"限制,但此函数不是其中之一。如果查看older version文档,无论您在Windows 8及更早版本中执行的操作,都会看到MAX_PATH是文档限制。

当在Windows 10(1607)中添加了新的长路径策略时,大多数功能都在灰色阴影框中的文档中添加了一个注释,此功能是放宽已存在的\\?\前缀文档,但对于此功能,策略和\\?\都是新的,但只有策略在带有版本信息的灰色框中,\\?\前缀是正常描述的一部分,所以它看起来总是如此在那里,但实际上与政策同时记录了!

答案 1 :(得分:2)

除了修改注册表外,还必须在应用程序清单中包含longPathAware设置。这在documentation

中有所描述
  

您还可以通过以下方式为每个应用启用新的长路径行为   manifest:XML

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

这些是不再具有的目录管理功能   如果您选择加入长路径行为,则MAX_PATH限制:   CreateDirectoryW,CreateDirectoryExW GetCurrentDirectoryW   RemoveDirectoryW SetCurrentDirectoryW。

您的问题中找到的清单无效。 application元素应位于assembly内。

<!-- language: xml -->

<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<assembly xmlns='urn:schemas-microsoft-com:asm.v1' manifestVersion='1.0'>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel level='asInvoker' uiAccess='false' />
      </requestedPrivileges>
    </security>
  </trustInfo>
  <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>
</assembly>

最后,任何提及都是Windows 7缺少重点。 MAX_PATH限制只能从Windows 10 1607中删除。

相关问题