即使在禁用验证后,OpenFileDialog 也不允许使用“/”字符

OpenFileDialog does not allow '/' character even after disabling validation

类似问题:How to avoid of file name validation in SaveFileDialog C#

我正在编写一个应用程序,该应用程序可以选择支持打开可执行文件并 运行 使用给定参数打开它们,我正在尝试使用 OpenFileDialog 作为一种用户友好的方式来实现这一点。禁用 AddExtensionValidateNamesCheckFileExistsCheckPathExists 后,我可以将参数传递给可执行文件,应用程序按预期使用参数运行它们。

但是,当我尝试传递包含 "invalid" 文件名字符(例如“/”)的参数时,我被阻止并收到此示例消息:

并且不允许继续,即使 ValidateNames 设置为 false。

这是关于对话框的代码:

OpenFileDialog dialog = new OpenFileDialog();
dialog.AddExtension = false;
dialog.CheckFileExists = false;
dialog.CheckPathExists = false;
dialog.ValidateNames = false;
if (dialog.ShowDialog() == DialogResult.OK) {
    //Parse text manually here (parsing is fully handled on the developer side)
}

有什么方法可以解决这个问题并完全禁用输入验证,还是我必须编写自定义文件对话框实现?

您无法覆盖默认验证。因此,您要么必须在单独的对话框中询问参数,要么自己重新实现文件打开对话框。