简体   繁体   中英

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

Suddenly System.IO was returning max paths exceptions. 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

<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. The only issue is that this is a web application. I am unable to figure out how to add the equivelant of an app.manifest to a web application.

You could try adding these lines inside your web.config after the <startup> end tag:

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

I am unable to figure out how to add the equivalent of an app.manifest to a web application.

Its not exactly standard practice, but you can add a manifest to an ASP .NET Web application. If you are using VS2017, it would look similar to the below screens:

在此处输入图像描述

You can try to change this setting using regedit:

  1. run regedit.exe as administrator
  2. locate [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem]
  3. change data value [LongPathsEnabled] (DWORD) to 1
  4. close regedit and restart Windows

This should solve the problem for ASP.NET also.

The registry key can also be controlled via Group Policy at
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. Can find more information in the link below:

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

Update for ASP.NET Core applications

.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. In such cases you can use ThePretendProgrammer solution:

  1. Add app.manifest file to the root of project
<?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
<PropertyGroup>
  <TargetFramework>net6.0</TargetFramework>
  <ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup>

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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