System.Windows.Forms.SaveFileDialog 不强制执行默认扩展名
我试图让 SaveFileDialog
和 FileOpenDialog
对用户输入的文件名强制执行扩展名。我尝试使用 问题 389070< 中提出的示例/a> 但它无法按预期工作:
var dialog = new SaveFileDialog())
dialog.AddExtension = true;
dialog.DefaultExt = "foo";
dialog.Filter = "Foo Document (*.foo)|*.foo";
if (dialog.ShowDialog() == DialogResult.OK)
{
...
}
如果用户在恰好存在文件 test.xml
的文件夹中键入文本 test
,对话框将建议名称 test.xml
(而我实际上只想在列表中看到 *.foo
)。更糟糕的是:如果用户选择 test.xml
,那么我确实会得到 test.xml
作为输出文件名。
如何确保 SaveFileDialog
确实只允许用户选择 *.foo
文件?或者至少,当用户单击“保存”时,它会替换/添加扩展程序?
建议的解决方案(实现 FileOk 事件处理程序)仅完成部分工作,因为如果文件名的扩展名错误,我真的想禁用“保存”按钮。
为了留在对话框中并更新FileOk
处理程序中文本框中显示的文件名,以反映具有正确扩展名的新文件名,请参阅< href="https://stackoverflow.com/questions/1599511/how-to-replace-filename-in-savefiledialog-fileok-event-handler">以下相关问题。
I am trying to make SaveFileDialog
and FileOpenDialog
enforce an extension to the file name entered by the user. I've tried using the sample proposed in question 389070 but it does not work as intended:
var dialog = new SaveFileDialog())
dialog.AddExtension = true;
dialog.DefaultExt = "foo";
dialog.Filter = "Foo Document (*.foo)|*.foo";
if (dialog.ShowDialog() == DialogResult.OK)
{
...
}
If the user types the text test
in a folder where a file test.xml
happens to exist, the dialog will suggest the name test.xml
(whereas I really only want to see *.foo
in the list). Worse: if the user selects test.xml
, then I will indeed get test.xml
as the output file name.
How can I make sure that SaveFileDialog
really only allows the user to select a *.foo
file? Or at least, that it replaces/adds the extension when the user clicks Save
?
The suggested solutions (implement the FileOk
event handler) only do part of the job, as I really would like to disable the Save
button if the file name has the wrong extension.
In order to stay in the dialog and update the file name displayed in the text box in the FileOk
handler, to reflect the new file name with the right extension, see the following related question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以处理
FileOk
事件,如果扩展名不正确则取消它You can handle the
FileOk
event, and cancel it if it's not the correct extensionAFAIK 没有可靠的方法来强制执行给定的文件扩展名。无论如何,最好在对话框关闭后验证扩展名是否正确,并在扩展名不匹配时通知用户他选择了无效文件。
AFAIK there's no reliable way to enforce a given file extension. It is a good practice anyway to verify the correct extension, once the dialog is closed and inform the user that he selected an invalid file if the extension doesn't match.
我最接近的方法是使用 FileOk 事件。例如:
查看 FileOK 事件在 MSDN 上。
The nearest I've got to this is by using the FileOk event. For example:
Checkout FileOK Event on MSDN.
我遇到了同样的问题,我可以通过执行以下操作来控制显示的内容:
使用 OpenFileDialog,过滤器字符串中的第一项是
SaveFileDialog 的默认值,过滤器中的第二项用作默认值:
将这两个过滤器与各自的文件对话框一起使用后,终于出现了预期的结果。默认情况下,当用户选择“保存”按钮并显示“保存文件”对话框时,所选文件类型是“保存文件”对话框的过滤器中定义的 Program X 文件类型。同样,为 openfiledialog 选择的文件类型是在 openfileDialog 的过滤器中定义的程序 X 文件类型。
正如本线程中上面提到的那样,最好进行一些输入验证。我只是想指出,两个对话框之间的过滤器似乎不同,即使它们都继承了 filedialog 类。
I ran into this same issue, and I was able to control what was shown by doing the following:
with the OpenFileDialog, the first item in the filter string was the default
with the SaveFileDialog, the second item in the filter was used as the default:
After having used these two filters with the respective fileDialogs, The expected results finally occurred. By default, when the user selects the save button and the savefiledialog shows up, the selected filetype is that of the Program X files type defined in the filter for the savefiledialog. Likewise the selected filetype for the openfiledialog is that of the Program X Files Type defined in the filter for the openfileDialog.
It would also be good to do some input validation as mentioned above in this thread. I just wanted to point out that the filters seem to be different between the two dialogs even though they both inherit the filedialog class.