System.Windows.Forms.SaveFileDialog不强制默认扩展 [英] System.Windows.Forms.SaveFileDialog does not enforce default extension

查看:530
本文介绍了System.Windows.Forms.SaveFileDialog不强制默认扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让 SaveFileDialog FileOpenDialog 执行的扩展,用户输入的文件名。我已经使用示例试图问题389070 <建议/ A>,但它不按预期工作:

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

如果用户键入文本测试其中一个文件测试的.xml 情况存在,对话框将建议的名称的test.xml (而我真的只是想看看 *包含.foo 列表)。更糟糕的是:如果用户选择的test.xml ,那么我会得到确实的test.xml 作为输出文件名。

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.

我怎样才能确保 SaveFileDialog 真的只允许用户选择 *包含.foo 文件?或者至少,它替换/添加扩展名时,用户点击保存

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?

建议的解决方案(实施在 FileOk 事件处理程序)只能做这项工作的一部分,我真的想禁用保存按钮,如果文件名有扩展名错误。

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.

为了留在对话框中的的更新中的<$显示在文本框中的文件名C $ C> FileOk 处理程序,以反映正确的扩展新的文件名,看的以下相关问题

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.

推荐答案

AFAIK有没有可靠的的方式来执行一个给定的文件扩展名。这是一个很好的做法反正来验证正确的扩展,一旦对话框关闭,并告知他选择了无效的文件,如果扩展名不匹配的用户。

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.

这篇关于System.Windows.Forms.SaveFileDialog不强制默认扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆