【问题标题】:SaveFileDialog error when cancelling the save dialog box取消保存对话框时的SaveFileDialog错误
【发布时间】:2016-05-09 20:08:17
【问题描述】:

我有一个保存文本文件的按钮,但是如果用户在保存对话框中选择取消,我会收到以下错误消息:

在 mscorlib.dll 中发生了“system.argumentexception”类型的未处理异常

补充信息:空路径名不合法。

Private sub cmdSave_Click (sender As object, e As EventArgs) Handles cmdSave.Click
    If rtfTextEditor.Text.Length > 0 then
      SaveFileDialog1.ShowDialog()
      System.IO.File.WriteAllText(SaveFileDialog1.Filename, rtfTextEditor.Text)
    End If
End Sub

【问题讨论】:

  • ShowDialog() 是一个返回结果的方法。您没有测试用户是否取消

标签: .net vb.net savefiledialog argumentexception


【解决方案1】:

当对话框被取消时,我假设SaveFileDialog1.FilenameNothing

你应该检查对话的结果:

If SaveFileDialog1.ShowDialog = DialogResult.OK Then
    System.IO.File.WriteAllText(SaveFileDialog1.Filename, rtfTextEditor.Text)
End If

【讨论】:

    【解决方案2】:

    如果在尝试保存文件之前使用ShowDialog 命令,您无需等待结果。

    SaveFileDialog1.Filename 的内容将为 null,这可能是错误的根源。您需要检查用户是否点击“保存”:

    If SaveFileDialog1.ShowDialog() == true then
        System.IO.File.WriteAllText(SaveFileDialog1.Filename, rtfTextEditor.Text)
    End If
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-11
      • 1970-01-01
      • 2012-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多