SaveFileDialog上的DialogResult.OK不起作用

我尝试,当我在SaveFileDialog按保存时,我做了一些事情。 我尝试修复但总是出错。

 SaveFileDialog dlg2 = new SaveFileDialog(); dlg2.Filter = "xml | *.xml"; dlg2.DefaultExt = "xml"; dlg2.ShowDialog(); if (dlg2.ShowDialog() == DialogResult.OK) {....} 

但我有错误 – 这说:

错误: ‘System.Nullable’不包含’OK’的定义,并且没有扩展方法’OK’可以找到接受类型’System.Nullable’的第一个参数(你是否缺少using指令或汇编引用?)

我尝试修复此代码:

 DialogResult result = dlg2.ShowDialog(); //here is error again if (result == DialogResult.OK) {....} 

现在错误是在DialogResult上说: ‘System.Windows.Window.DialogResult’是’属性’但是像’类型’一样使用

我假设您指的是WPF而不是Windows Form以下是使用SaveFileDialog示例

 //configure save file dialog box Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog(); dlg.FileName = "Document"; //default file name dlg.DefaultExt = ".xml"; //default file extension dlg.Filter = "XML documents (.xml)|*.xml"; //filter files by extension // Show save file dialog box Nullable result = dlg.ShowDialog(); // Process save file dialog box results if (result == true) { // Save document string filename = dlg.FileName; } 

其他例子

WPF您必须处理DialogResult Enumeration和Window.DialogResult属性之间的冲突

尝试使用完全限定名称来引用枚举:

 System.Windows.Forms.DialogResult result = dlg2.ShowDialog(); if (result == DialogResult.OK) {....}