首页 > 解决方案 > 如何使用 Microsoft.Win32.SaveFileDialog 保存文件名超过 MAX_PATH 的文件?

问题描述

我正在尝试保存文件:

Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog
        {
            FileName = "verylongexampleverylongexampleverylongexampleverylongexampleverylongexampleverylongexampleverylongexampleverylongexampleverylongexampleverylongexampleverylongexampleverylongexampleverylongexampleverylongexampleverylongexampleverylongexampleverylongexampleverylongexampleverylongexampleverylongexampleverylongexampleverylongexampleverylongexampleverylongexampleverylongexampleverylongexampleverylongexampleverylongexampleverylongexampleverylongexampleverylongexampleverylongexampleverylongexample.foo",
            DefaultExt = ".foo",
            Filter = "Foo Bar|*.foo"
        };

在对话框中按保存后,我收到以下信息:

The filename, directory name, or volume label syntax is incorrect.

即使我有这个app.manifest

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

如此处所述:https ://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#maximum-path-length-limitation

标签: c#wpfwindowswinapimax-path

解决方案


所以是的,评论中提到的那些人意味着你的 Windows 不支持它。

从 Windows 10版本 1607开始,MAX_PATH 限制已从常见的 Win32 文件和目录函数中删除。

现在,要使其跨 Windows,您必须创建自定义文件对话框,通过使用\\?\Drive:\样式的路径进行操作。


推荐阅读