简体   繁体   中英

saveFileDialog: The file name is not valid

Creating an application in Visual Studio 2015 using C#, I want to save a file to wherever the user wants so I've add a SaveFileDialog object to my project but after setting the properties, whatever file name I enter, the save dialog says:

The file name is not valid.

(I checked the file path and characters in the file name).

My code to show the dialog:

DialogResult dr = saveFileDialog1.ShowDialog();
if (dr == System.Windows.Forms.DialogResult.OK)
{
     Save(saveFileDialog1.FileName);
}

What is the problem?

在此处输入图片说明

在此处输入图片说明

You should remove the double quotes around your Filter property.

in code it should be

saveFileDialog1.Filter = "Product list|*.json";

The double quotes seems to confuse the SaveFileDialog checks on the validity of the name typed. (Notice how the text appears in the 'Save as Type' combobox).
Also, if you type the file name complete with the extension, this error seems to disappear.

At first it seems that your particular Filter text is causing the problem because it is interpreted as a description instead of the mandatory description followed by a | and an extension as required by the Filter specs.

But also if you write it like this

saveFileDialog1.Filter = "\"Product files|*.json\"|*.json";

you will get an Argument Exception error saying that the supplied Filter string lacks of the required format.

So the only conclusion here is that you cannot use double quotes in the Filter string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM