OpenFileDialog 在超过 260 个字符的路径上返回空字符串(或根本不返回)

cdi*_*ker 5 .net c# openfiledialog max-path

我正在编写一个程序,需要从系统上的任何地方读取文件。该程序的某些用户的路径超过 260 个字符的限制。在OpenFileDialog不使用文件超过260个字符的路径。

我试过同时使用System.Windows.Forms.OpenFileDialogMicrosoft.Win32.OpenFileDialog。对于前者,当我导航到并选择文件后单击“打开”时,窗口不会关闭,程序也不会继续。在后者的情况下,当我单击“打开”时窗口将关闭,但路径是一个空字符串。

我已经更新了我电脑上的注册表。我已经编辑了应用程序清单文件。我会尝试将“//?/”字符串添加到我的路径中,但没有要添加到的路径。

var dialog = new OpenFileDialog
{
  // initialize dialog
}

if (dialog.ShowDialog() == DialogResult.OK) // DialogResult.OK replaced with true if using Microsoft.Win32.OpenFileDialog
{
  // if when using System.Windows.Forms.OpenFileDialog, I will never get to this point
  // if using Microsoft.Win32.OpenFileDialog, I will get here but dialog.FileNames will be empty
}
Run Code Online (Sandbox Code Playgroud)

如果我更新了注册表和应用程序清单,我希望上面的代码在长路径和短路径下都能正常工作。我怀疑这只是不受支持,但我所有的搜索都表明人们提供的解决方案要么不起作用,要么仅适用于特定情况。

cdi*_*ker 1

在这种情况下,System.Windows.Forms.OpenFileDialog我能够通过设置ValidateNames为 false 来解决ShowDialog()用户单击“打开”时不返回的问题,

    System.Windows.Forms.OpenFileDialog openFileDialog_WindowsForms = new System.Windows.Forms.OpenFileDialog
    {
        CheckFileExists = true,
        CheckPathExists = true,
        ValidateNames = false // this will allow paths over 260 characters
    };

    if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        string[] fileNames = openFileDialog_WindowsForms.getFileNames_WindowsForms();

        foreach (var file in fileNames)
        {
            try
            {
                Console.WriteLine(File.ReadAllText(file));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Couldn't open file from Windows.Forms.OpenFileDialog:" + ex.Message);
            }
        }

    };
Run Code Online (Sandbox Code Playgroud)

FilePath和反射来克服无法从或属性访问的路径FilePaths。事实证明,这些路径存在于私有财产中,我可以使用反射来访问它。

public static class OpenFileDialogLongPathExtension
{
    public static string[] getFileNames_WindowsForms(this System.Windows.Forms.OpenFileDialog dialog)
    {
        var fileNamesProperty = dialog.GetType().GetProperty("FileNamesInternal", BindingFlags.NonPublic | BindingFlags.Instance);
        var fileNamesFromProperty = (string[])fileNamesProperty?.GetValue(dialog);
        return fileNamesFromProperty;
    }
}
Run Code Online (Sandbox Code Playgroud)

我尝试了类似的方法Microsoft.Win32.OpenFileDialog,但似乎私有财产仍然无效,因此相同的解决方案不起作用。

不管怎样,我希望这对其他人有帮助。此示例是使用 .NET Framework 4.8 创建的。