超过 260 个字符的路径上的 OpenFileDialog returns 空字符串(或者根本没有 return)
OpenFileDialog returns empty string on paths over 260 characters (or doesn't return at all)
我正在编写一个程序,需要从系统的任何位置读取文件。该程序的某些用户的路径超过了 260 个字符的限制。 OpenFileDialog
不适用于路径超过 260 个字符的文件。
我试过同时使用 System.Windows.Forms.OpenFileDialog
和 Microsoft.Win32.OpenFileDialog
。对于前者,当我在导航到并选择文件后单击 "open" 时,window 不会关闭并且程序不会继续。对于后者,当我点击"open"时,window会关闭,但是路径是一个空字符串。
我已经更新了计算机上的注册表。我已经编辑了应用程序清单文件。我会尝试将“//?/”字符串添加到我的路径中,但没有要添加的路径。
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
}
如果我更新了注册表和应用程序清单,我希望上面的代码在长路径和短路径上都能正常工作。我怀疑这只是不受支持,但我的所有搜索都显示人们提供的解决方案要么不起作用,要么只在特定情况下有效。
在 System.Windows.Forms.OpenFileDialog
的情况下,我可以通过将 ValidateNames
设置为 false 来解决 ShowDialog()
在用户单击 "open" 时不返回的问题,
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);
}
}
};
和反射以克服无法从 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;
}
}
我为 Microsoft.Win32.OpenFileDialog
尝试了类似的方法,但似乎 private 属性 仍然无效,因此相同的解决方案不起作用。
无论如何,我希望这对其他人有所帮助。此示例是使用 .NET Framework 4.8 创建的。
我正在编写一个程序,需要从系统的任何位置读取文件。该程序的某些用户的路径超过了 260 个字符的限制。 OpenFileDialog
不适用于路径超过 260 个字符的文件。
我试过同时使用 System.Windows.Forms.OpenFileDialog
和 Microsoft.Win32.OpenFileDialog
。对于前者,当我在导航到并选择文件后单击 "open" 时,window 不会关闭并且程序不会继续。对于后者,当我点击"open"时,window会关闭,但是路径是一个空字符串。
我已经更新了计算机上的注册表。我已经编辑了应用程序清单文件。我会尝试将“//?/”字符串添加到我的路径中,但没有要添加的路径。
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
}
如果我更新了注册表和应用程序清单,我希望上面的代码在长路径和短路径上都能正常工作。我怀疑这只是不受支持,但我的所有搜索都显示人们提供的解决方案要么不起作用,要么只在特定情况下有效。
在 System.Windows.Forms.OpenFileDialog
的情况下,我可以通过将 ValidateNames
设置为 false 来解决 ShowDialog()
在用户单击 "open" 时不返回的问题,
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);
}
}
};
和反射以克服无法从 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;
}
}
我为 Microsoft.Win32.OpenFileDialog
尝试了类似的方法,但似乎 private 属性 仍然无效,因此相同的解决方案不起作用。
无论如何,我希望这对其他人有所帮助。此示例是使用 .NET Framework 4.8 创建的。