我正在尝试制作一个保存文本文件的简单Windows窗体应用程序。我在下面的程序上遇到了麻烦,它给了我:
空路径是不合法的
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();
}
}
}发布于 2015-10-20 06:25:01
因为您还没有“显示”SaveFileDialog,所以fileName是空的。
尝试将showDialog向上移动:
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);
}
}发布于 2015-10-20 07:11:16
您应该将写入语句包装如下。
if(file.ShowDialog()== DialogResult.OK)
File.WriteAllText(file.FileName, richTextBox1.Text);https://stackoverflow.com/questions/33229379
复制相似问题