在.NET 4.7中使用长路径时出现DirectoryNotFoundException [英] DirectoryNotFoundException when using long paths in .NET 4.7

查看:47
本文介绍了在.NET 4.7中使用长路径时出现DirectoryNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已在本地组策略编辑器中将 Enable Win32 Long Paths 设置为 Enabled ,然后重新启动计算机.

I have set Enable Win32 Long Paths in the Local Group Policy Editor to Enabled and restarted the computer.

这是代码:

string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
for (int i = 0; i < 10; i++)
    path += "\\" + new string('z', 200);
Directory.CreateDirectory(path);

我遇到了错误:

System.IO.DirectoryNotFoundException:'无法找到路径'C:\ Users ... \ Desktop \ zzzzzzzzzzzz ...

System.IO.DirectoryNotFoundException: 'Could not find a part of the path 'C:\Users...\Desktop\zzzzzzzzzz...

(实际上是一条奇怪的错误消息.)

(Which is actually a strange error message.)

app.config已经具有:

app.config already has:

<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7" />


更多信息(可能不重要)

我尝试按照此帖子和其他位置(尽管注释中指出,使用.net 4.7时并不需要)在 configuration 下的app.config中:

I tried adding as mentioned in this post and elsewhere (though as pointed out in the comments it's not needed when using .net 4.7) in app.config under configuration:

<runtime>
  <AppContextSwitchOverrides value="Switch.System.IO.UseLegacyPathHandling=false;Switch.System.IO.BlockLongPaths=false" />
</runtime>

仍然是相同的错误.

如果我只使用一个 zzzzzz ... ,它将在桌面上正确创建它.

If I only use one zzzzzz... it creates it on the desktop with no error.

我正在使用VS2017,Windows10.我尝试使用Winforms和WPF.

I'm using VS2017, Windows 10. I tried Winforms and WPF.

推荐答案

周年更新(RS1)有一个错误,该错误使长路径无需清单即可工作.对于任何更新的Windows,必须将应用程序清单文件"项添加到项目中.否则它将无法正常工作.

The Anniversary Update (RS1) has a bug that allow long paths to work without the manifest. For any updated Windows you must add the Application Manifest File item to your project. Otherwise it will not work.

<application xmlns="urn:schemas-microsoft-com:asm.v3">
  <windowsSettings>
    <longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
  </windowsSettings>
</application>

这篇关于在.NET 4.7中使用长路径时出现DirectoryNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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