简体   繁体   English

在 Web 应用程序上使用 .NET 4.6.2 的长路径问题

[英]Issues with Long Paths using .NET 4.6.2 on a web application

I am building an ASP.NET web application that needs to work with long paths.我正在构建一个需要使用长路径的 ASP.NET Web 应用程序。 I have it targeting .NET 4.6.2 and everything was perfect since 4.6.2 has long paths turned on by default ( Source ), until the Windows 10 creators (RS2) update.我有它针对 .NET 4.6.2 并且一切都很完美,因为 4.6.2 默认打开了长路径( Source ),直到 Windows 10 创建者(RS2)更新。

Suddenly System.IO was returning max paths exceptions.突然 System.IO 返回最大路径异常。 I did some research and found This post .我做了一些研究,发现了这篇文章 It seems that post Windows 10 creators (RS2) update, in order for long paths to work it is a requirement to have an app.mainfest with似乎发布了 Windows 10 创建者 (RS2) 更新,为了获得较长的工作路径,需要有一个 app.mainfest

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

I have verified that adding the app.manifest modification to a console app brings the long path functionality back.我已经验证将 app.manifest 修改添加到控制台应用程序可以恢复长路径功能。 The only issue is that this is a web application.唯一的问题是这是一个 Web 应用程序。 I am unable to figure out how to add the equivelant of an app.manifest to a web application.我无法弄清楚如何将 app.manifest 的等价物添加到 Web 应用程序中。

You could try adding these lines inside your web.config after the <startup> end tag:您可以尝试在<startup>结束标记之后在web.config中添加这些行:

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

Source: .NET 4.6.2 and long paths on Windows 10 (MSDN)来源: .NET 4.6.2 和 Windows 10 上的长路径(MSDN)

I am unable to figure out how to add the equivalent of an app.manifest to a web application.我无法弄清楚如何将等效的 app.manifest 添加到 Web 应用程序中。

Its not exactly standard practice, but you can add a manifest to an ASP .NET Web application.它不完全是标准做法,但您可以将清单添加到 ASP .NET Web 应用程序。 If you are using VS2017, it would look similar to the below screens:如果您使用的是 VS2017,它看起来类似于以下屏幕:

在此处输入图像描述

You can try to change this setting using regedit:您可以尝试使用 regedit 更改此设置:

  1. run regedit.exe as administrator管理员身份run regedit.exe
  2. locate [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem]定位[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem]
  3. change data value [LongPathsEnabled] (DWORD) to 1将数据值[LongPathsEnabled] (DWORD)更改为1
  4. close regedit and restart Windows关闭注册表并重新启动Windows

This should solve the problem for ASP.NET also.这也应该解决 ASP.NET 的问题。

The registry key can also be controlled via Group Policy at注册表项也可以通过组策略控制
Computer Configuration > Administrative Templates > System > Filesystem > Enable NTFS long paths . Computer Configuration > Administrative Templates > System > Filesystem > Enable NTFS long paths

You can try prefixing the file path with \\?\ to specify its a long file path exceeding the MAX_PATH which will be 260 characters.您可以尝试使用\\?\为文件路径添加前缀,以指定其长文件路径超过 MAX_PATH,即 260 个字符。 Can find more information in the link below:可以在下面的链接中找到更多信息:

https://docs.microsoft.com/en-gb/windows/desktop/FileIO/naming-a-file#maximum-path-length-limitation https://docs.microsoft.com/en-gb/windows/desktop/FileIO/naming-a-file#maximum-path-length-limitation

Update for ASP.NET Core applications ASP.NET Core 应用程序更新

.NET Core handles long paths correctly by default, but If the project uses external libraries (for example written in C) the problem can still exist. .NET Core 默认正确处理长路径,但如果项目使用外部库(例如用 C 编写),问题仍然存在。 In such cases you can use ThePretendProgrammer solution:在这种情况下,您可以使用ThePretendProgrammer解决方案:

  1. Add app.manifest file to the root of projectapp.manifest文件添加到项目的根目录
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
  
  <!-- Makes the application long-path aware. -->
  <!-- See https://docs.microsoft.com/windows/win32/fileio/maximum-file-path-limitation  -->
  <application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings>
      <longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
    </windowsSettings>
  </application>

</assembly>
  1. Add <ApplicationManifest>app.manifest</ApplicationManifest> to project settings<ApplicationManifest>app.manifest</ApplicationManifest>添加到项目设置中
<PropertyGroup>
  <TargetFramework>net6.0</TargetFramework>
  <ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup>

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM