9

我正在尝试对用户输入的文件名进行扩展SaveFileDialogFileOpenDialog强制执行。我尝试使用问题 389070中提出的示例,但它没有按预期工作:

var dialog = new SaveFileDialog())

dialog.AddExtension = true;
dialog.DefaultExt = "foo";
dialog.Filter = "Foo Document (*.foo)|*.foo";

if (dialog.ShowDialog() == DialogResult.OK)
{
    ...
}

如果用户在恰好存在test文件的文件夹中键入文本,对话框将建议名称(而我真的只想在列表中看到)。更糟糕的是:如果用户选择,那么我确实会得到输出文件名。test.xmltest.xml*.footest.xmltest.xml

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

建议的解决方案(实现FileOk事件处理程序)仅完成部分工作,因为如果文件名的扩展名错误,我真的想禁用该Save按钮。

为了留在对话框中更新FileOk处理程序中文本框中显示的文件名,以反映具有正确扩展名的新文件名,请参阅以下相关问题

4

5 回答 5

17

您可以处理该FileOk事件,如果它不是正确的扩展名,则将其取消

private saveFileDialog_FileOk(object sender, CancelEventArgs e)
{
    if (!saveFileDialog.FileName.EndsWith(".foo"))
    {
        MessageBox.Show("Please select a filename with the '.foo' extension");
        e.Cancel = true;
    }
}
于 2009-11-02T14:23:49.963 回答
3

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

于 2009-10-21T06:36:03.160 回答
0

我最接近的方法是使用 FileOk 事件。例如:

dialog.FileOk += openFileDialog1_FileOk;

private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
  if(!dialog.FileName.EndsWith(".foo"))
  { 
     e.Cancel = true;
  }
}

签出 MSDN 上的FileOK 事件

于 2009-11-02T14:26:24.450 回答
0

我遇到了同样的问题,我可以通过执行以下操作来控制显示的内容:

使用 OpenFileDialog,过滤器字符串中的第一项是默认值

openFileDialog1.Filter = "Program x Files (*.pxf)|*.pxf|txt files (*.txt)|*.txt";
openFileDialog1.ShowDialog();

使用 SaveFileDialog,过滤器中的第二项被用作默认值:

SaveFileDialog saveFileDialog1 = new SaveFileDialog();

saveFileDialog1.Filter = "txt files (*.txt)|*.txt|Program x Files (*.pxf)|*.pxf";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
    if (saveFileDialog1.FileName != null)
    {
        // User has typed in a filename and did not click cancel
        saveFile = saveFileDialog1.FileName;
        MessageBox.Show(saveFile);
        saveCurrentState();

    }
} 

在将这两个过滤器与各自的 fileDialogs 一起使用后,最终出现了预期的结果。默认情况下,当用户选择保存按钮并显示 savefiledialog 时,所选文件类型是 savefiledialog 过滤器中定义的 Program X 文件类型。同样,openfiledialog 的选定文件类型是 openfileDialog 过滤器中定义的 Program X 文件类型的文件类型。

如上所述在这个线程中进行一些输入验证也是很好的。我只是想指出两个对话框之间的过滤器似乎不同,即使它们都继承了 filedialog 类。

于 2012-02-19T04:55:43.540 回答
-1
    //this must be ran as administrator due to the change of a registry key, but it does work...

    private void doWork()
    {
        const string lm = "HKEY_LOCAL_MACHINE";
        const string subkey = "\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\AutoComplete";
        const string keyName = lm + subkey;

        int result = (int)Microsoft.Win32.Registry.GetValue(keyName, "AutoComplete In File Dialog", -1);

        MessageBox.Show(result.ToString());

        if(result.ToString() == "-1")
        {
            //-1 means the key does not exist which means we must create one...
            Microsoft.Win32.Registry.SetValue(keyName, "AutoComplete In File Dialog", 0);
            OpenFileDialog ofd1 = new OpenFileDialog();
            ofd1.ShowDialog();
        }
        if (result == 0)
        {
            //The Registry value is already Created and set to '0' and we dont need to do anything
        }
    }
于 2012-02-25T03:00:07.753 回答