为什么在 .NET 4.7.2 下我仍然有一个 DirectoryNotFoundException 因为 Path Too Long?

Why do I still have a DirectoryNotFoundException because of Path Too Long under .NET 4.7.2?

.NET 4.6.2 在 System.IO API 中宣布 it removed the maximum path length restriction 260 个字符。

The absence of targeting the .NET Framework 4.6.2 or setting the AppContext switch results in the existing behavior of being blocked from using paths longer than MAXPATH. The behavior is opt-in to maintain backwards compatibility for existing applications.

(强调我的)。

我想在 .NET 4.7.2 下,我将不再需要编写故障保护来防止这个 MAXPATH 问题。

但是,一旦 sourceDirectoryName 大于 260 个字符,此代码将抛出 System.IO.DirectoryNotFoundException

var sourceDirectoryName = @"C:\Users\MYUSER___\Documents\Sandbox\temp\OneDrive_1_11-04-2022\MySuperProject_With_Long_Name_Prefix\aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.bundle";
var destinationArchiveFileName = @"C:\Users\MYUSER___\Documents\Sandbox\temp\OneDrive_1_11-04-2022\MySuperProject_With_Long_Name_Prefix\aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.bundle.out";
ZipFile.CreateFromDirectory(sourceDirectoryName, destinationArchiveFileName);

为什么我在 .NET 4.7.2 下仍然遇到大路径问题?

也许我读错了那句话,

The absence of targeting the .NET Framework 4.6.2 or setting the AppContext switch results in the existing behavior of being blocked from using paths longer than MAXPATH. The behavior is opt-in to maintain backwards compatibility for existing applications.

但在我看来,以 .NET 4.7.2 为目标的简单事实会导致 允许 路径长于 MAXPATH 的行为。

然而,我不得不在 app.manifest

中手动启用它
<application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings>
      <longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
    </windowsSettings>
  </application>

代码不再触发任何异常。