简体   繁体   English

saveFileDialog:文件名无效

[英]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:使用 C# 在 Visual Studio 2015 中创建应用程序,我想将文件保存到用户想要的任何位置,因此我已将SaveFileDialog对象添加到我的项目中,但在设置属性后,无论我输入什么文件名,保存对话框都显示:

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.您应该删除 Filter 属性周围的双引号。

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.双引号似乎混淆了 SaveFileDialog 对输入的名称有效性的检查。 (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.您将收到一个 Argument Exception 错误,指出提供的过滤器字符串缺少所需的格式。

So the only conclusion here is that you cannot use double quotes in the Filter string.所以这里唯一的结论是不能在过滤字符串中使用双引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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