c# - OpenFileDialog 在超过 260 个字符的路径上返回空字符串(或根本不返回)
问题描述
我正在编写一个程序,该程序需要从系统上的任何位置读取文件。该程序的某些用户的路径超过了 260 个字符的限制。OpenFileDialog
不适用于路径超过 260 个字符的文件。
我试过同时使用System.Windows.Forms.OpenFileDialog
和Microsoft.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
}
如果我更新了注册表和应用程序清单,我希望上面的代码在长路径和短路径上都能正常工作。我怀疑这只是不支持,但我所有的搜索都显示人们提供的解决方案要么不起作用,要么只在特定情况下起作用。
解决方案
在这种情况下,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);
}
}
};
和反射来克服无法从FilePath
orFilePaths
属性访问的路径。事实证明,路径存在于我可以使用反射访问的私有属性中。
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
,但似乎私有财产仍然无效,所以相同的解决方案不起作用。
无论哪种方式,我希望这对其他人有所帮助。此示例是使用 .NET Framework 4.8 创建的。
推荐阅读
- html - 3 位于左侧的列页脚 - html
- bash - 使用 AZ cli 根据 AD 组分配特定角色
- kubernetes - Kubernetes 为一个节点批量启动 Pod
- python-3.x - 为 gensim FastText 无监督模型准备大的 txt 文件
- flutter - Flutter Web - 托管时图标不显示
- java - 如何在 Java 中恢复 Kotlin 协程延续
- r - 在 Shiny 应用程序的反应函数中使用 if else 语句
- python - 如何在 jupyter notebook 上制作群组栏?
- android - Android分页流动泄漏视图模型和片段
- android - 如何在屏幕关闭并按下主页时保持 Android 服务处于活动状态?