首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SaveFileDialog错误

SaveFileDialog错误
EN

Stack Overflow用户
提问于 2015-10-20 06:21:25
回答 2查看 549关注 0票数 1

我正在尝试制作一个保存文本文件的简单Windows窗体应用程序。我在下面的程序上遇到了麻烦,它给了我:

空路径是不合法的

代码语言:javascript
复制
namespace Filing
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button_Save_Click(object sender, EventArgs e)
        {

            SaveFileDialog file = new SaveFileDialog();

            file.Filter = "Text (*.txt) | Word File *.doc";
            file.Title = "Save a file";
            File.WriteAllText(file.FileName, richTextBox1.Text);

            file.ShowDialog();
        }

        private void button_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-10-20 06:25:01

因为您还没有“显示”SaveFileDialog,所以fileName是空的。

尝试将showDialog向上移动:

代码语言:javascript
复制
private void button_Save_Click(object sender, EventArgs e)
    {

        SaveFileDialog file = new SaveFileDialog();

        file.Filter = "Text (*.txt) | Word File *.doc";
        file.Title = "Save a file";
        //Ask the user to select the file path and file name, don't forget to handle cancel button!
        if(file.ShowDialog() != DialogResult.Cancel)
        {
              File.WriteAllText(file.FileName, richTextBox1.Text);
        }
    }
票数 1
EN

Stack Overflow用户

发布于 2015-10-20 07:11:16

您应该将写入语句包装如下。

代码语言:javascript
复制
if(file.ShowDialog()== DialogResult.OK)
     File.WriteAllText(file.FileName, richTextBox1.Text);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33229379

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档