代码之家  ›  专栏  ›  技术社区  ›  Pierre Arnaud

系统。窗户。形式。SaveFileDialog不强制使用默认扩展名

  •  9
  • Pierre Arnaud  · 技术社区  · 16 年前

    我正在努力 SaveFileDialog FileOpenDialog question 389070 但它并没有按预期工作:

    var dialog = new SaveFileDialog())
    
    dialog.AddExtension = true;
    dialog.DefaultExt = "foo";
    dialog.Filter = "Foo Document (*.foo)|*.foo";
    
    if (dialog.ShowDialog() == DialogResult.OK)
    {
        ...
    }
    

    如果用户键入文本 test test.xml 如果存在,对话框将建议该名称 (而我真的只想看看 *.foo 在列表中)。更糟糕的是:如果用户选择 作为输出文件名。

    我怎样才能确保 实际上只允许用户选择 *.foo 文件?或者至少,当用户点击时,它会替换/添加扩展名 Save

    建议的解决方案(实施 FileOk 事件处理程序)只执行部分工作,因为我真的很想禁用 如果文件名的扩展名错误,请单击按钮。

    为了留在对话中 更新文本框中显示的文件名 文件OK 处理程序,要反映具有正确扩展名的新文件名,请参阅 following related question .

    5 回复  |  直到 8 年前
        1
  •  3
  •   Darin Dimitrov    16 年前

    你可以处理 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;
        }
    }
    
        2
  •  17
  •   Thomas Levesque    16 年前

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

        3
  •  0
  •   Ian    16 年前

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

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

    FileOK Event

        4
  •  0
  •   John Bartels    14 年前

    使用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一起使用后,最终出现了预期的结果。默认情况下,当用户选择保存按钮并显示保存对话框时,所选的文件类型是在保存对话框的过滤器中定义的Program X文件类型。同样,为openfiledialog选择的文件类型是openfiledialog过滤器中定义的Program X文件类型。

        5
  •  -1
  •   John Bartels    14 年前
        //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
            }
        }
    
    推荐文章