.NET System.IO.PathTooLongException from Web Appli

2019-08-14 07:57发布

问题:

Windows 10 and Windows Server 2016 introduce solution for the traditional Long Path issue. The solution is straightforward to implement and detailed very good in the following blog post. Following the steps works successfully for a .NET console/desktop application. However, for some reason, when running the same code from a ASP.NET web application I still getting the same classic System.IO.PathTooLongException exception.

The code that throws exception:

Directory.CreateDirectory(longPath);

As I mention, the code runs successfully on console application, but fails in ASP.NET website application. the website web.config includes the following:

<?xml version="1.0"?>
<configuration>
  <runtime>
    <AppContextSwitchOverrides value="Switch.System.IO.UseLegacyPathHandling=false;Switch.System.IO.BlockLongPaths=false"/>
  </runtime>
</configuration>

and application manifest file as explained in the blog link above.

Any idea appreciated.

回答1:

Thanks to @bradbury9 that point me to similar issue, I confirmed that the application failed to load the switch long-path blockage settings in run-time. In addition, because the version of my application based on .NET 4.5.1, it's impossible to use the AppContext class for manually set the desired switches programmatically.

Solution:

For ASP.NET web application, based on .NET 4.6.1 or below, make sure that .NET 4.6.2 is also installed on the machine that runs the application, and add the following attribute targetFramework="4.6.2" the the httpRuntime configuration.

Example:

<system.web>
    <httpRuntime targetFramework="4.6.2" />
    <compilation targetFramework="4.5.1" />
</system.web>

* Please notice that Windows that supports Long Path, such Windows Server 2016 and Windows 10, will have already .NET 4.6.2 installed