空路径名不合法

发布于 2024-10-19 12:50:24 字数 1524 浏览 1 评论 0原文

我有一个“保存”按钮,因此当用户单击时,它将保存 xml 文件(xml 序列化)。此处使用了保存文件对话框,当我按取消而不选择任何文件时,会出现“参数异常”并显示“空路径名不合法”。我该如何处理这个异常?我希望即使没有在保存文件对话框中选择任何路径,表单也能保持不变。非常感谢。

我的 savefiledialog 片段:

private void SaveButton_Click(object sender, RoutedEventArgs e)
{
        string savepath;
        SaveFileDialog DialogSave = new SaveFileDialog();
        // Default file extension
        DialogSave.DefaultExt = "txt";
        // Available file extensions
        DialogSave.Filter = "XML file (*.xml)|*.xml|All files (*.*)|*.*";
        // Adds a extension if the user does not
        DialogSave.AddExtension = true;
        // Restores the selected directory, next time
        DialogSave.RestoreDirectory = true;
        // Dialog title
        DialogSave.Title = "Where do you want to save the file?";
        // Startup directory
        DialogSave.InitialDirectory = @"C:/";
        DialogSave.ShowDialog();
        savepath = DialogSave.FileName;
        DialogSave.Dispose();
        DialogSave = null;
        ...
        using (Stream savestream = new FileStream(savepath, FileMode.Create))
        {
                XmlSerializer serializer = new XmlSerializer(typeof(FormSaving));
                serializer.Serialize(savestream, formsaving);
        }

}

我的参数异常发生在这一行:

using (Stream savestream = new FileStream(savepath, FileMode.Create))
{
        XmlSerializer serializer = new XmlSerializer(typeof(FormSaving));
        serializer.Serialize(savestream, formsaving);
}

I have a "save" button so when users click, it will do a saving of xml file(xml serialization). A savefiledialog is used here and when i press cancel without selecting any file an "Argument Exception" occurs and says "Empty path name is not legal". How do i handle this exception? I would like the form to remain the same even without any path selected in the savefiledialog. Many thanks.

My savefiledialog snippet:

private void SaveButton_Click(object sender, RoutedEventArgs e)
{
        string savepath;
        SaveFileDialog DialogSave = new SaveFileDialog();
        // Default file extension
        DialogSave.DefaultExt = "txt";
        // Available file extensions
        DialogSave.Filter = "XML file (*.xml)|*.xml|All files (*.*)|*.*";
        // Adds a extension if the user does not
        DialogSave.AddExtension = true;
        // Restores the selected directory, next time
        DialogSave.RestoreDirectory = true;
        // Dialog title
        DialogSave.Title = "Where do you want to save the file?";
        // Startup directory
        DialogSave.InitialDirectory = @"C:/";
        DialogSave.ShowDialog();
        savepath = DialogSave.FileName;
        DialogSave.Dispose();
        DialogSave = null;
        ...
        using (Stream savestream = new FileStream(savepath, FileMode.Create))
        {
                XmlSerializer serializer = new XmlSerializer(typeof(FormSaving));
                serializer.Serialize(savestream, formsaving);
        }

}

My argument exception occurs at this line:

using (Stream savestream = new FileStream(savepath, FileMode.Create))
{
        XmlSerializer serializer = new XmlSerializer(typeof(FormSaving));
        serializer.Serialize(savestream, formsaving);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

じ违心 2024-10-26 12:50:24

这里的问题是,您不关心“保存”对话框的结果,即使用户单击“取消”,您也会尝试保存。您应该将代码更改为如下所示:

...
DialogSave.InitialDirectory = @"C:/";
if( DialogSave.ShowDialog() == DialogResult.OK )
{
  savepath = DialogSave.FileName;
  DialogSave = null;
  ...
  using (Stream savestream = new FileStream(savepath, FileMode.Create))
  {
     XmlSerializer serializer = new XmlSerializer(typeof(FormSaving));
     serializer.Serialize(savestream, formsaving);
  }
}
DialogSave.Dispose();

The problem here is that you do not care about the result of the Save dialog, and you try to save even if the user clicked Cancel. You should change the code to look something like this instead:

...
DialogSave.InitialDirectory = @"C:/";
if( DialogSave.ShowDialog() == DialogResult.OK )
{
  savepath = DialogSave.FileName;
  DialogSave = null;
  ...
  using (Stream savestream = new FileStream(savepath, FileMode.Create))
  {
     XmlSerializer serializer = new XmlSerializer(typeof(FormSaving));
     serializer.Serialize(savestream, formsaving);
  }
}
DialogSave.Dispose();
九歌凝 2024-10-26 12:50:24

如果用户取消对话框,您可能不想保存?检查 ShowDialog 的结果并采取相应措施:

if (DialogSave.ShowDialog() == true)
{
    savepath = DialogSave.FileName;
            ...
    using (Stream savestream = new FileStream(savepath, FileMode.Create))
    {
        XmlSerializer serializer = new XmlSerializer(typeof(FormSaving));
        serializer.Serialize(savestream, formsaving);
    }
}

You probably don't want to save if the user cancels the dialog? Check for the result from ShowDialog and act accordingly:

if (DialogSave.ShowDialog() == true)
{
    savepath = DialogSave.FileName;
            ...
    using (Stream savestream = new FileStream(savepath, FileMode.Create))
    {
        XmlSerializer serializer = new XmlSerializer(typeof(FormSaving));
        serializer.Serialize(savestream, formsaving);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文