OpenFileDialog does not allow '/' characte

2019-04-16 16:23发布

问题:

Similar question: How to avoid of file name validation in SaveFileDialog C#

I am writing an application that has an option to support opening executables and running them with given parameters, and I am trying to use OpenFileDialog as a user-friendly way to achieve this. After disabling AddExtension, ValidateNames, CheckFileExists and CheckPathExists, I can pass parameters to executables and the application runs them using the arguments as intended.

However, when I attempt to pass parameters that include "invalid" filename characters (such as '/'), I am stopped and get this example message:

and am not allowed to continue, even though ValidateNames is set to false.

Here is the code concerning the dialog:

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)
}

Is there any way to resolve this and completely disable input validation, or do I have to write a custom file dialog implementation?

回答1:

You can't override the default validation. As such you will either have to ask for the parameters in a separate dialogue or re-implement the file open dialogue yourself.