空路径名不合法错误 [英] empty path name is not legal Error
本文介绍了空路径名不合法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在打开对话框中单击取消"后单击打开或保存命令n时在记事本应用程序上工作,但出现错误.
im working on a notepad application when i click on open or save command n after click on cancel in opendialoge box i got error.
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = "Open";
dlg.Filter = "Text Documents(*.*)|*.*|All Files(*.*)|*.*";
dlg.ShowDialog();
richTextBox1.LoadFile(dlg.FileName);
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog dlg = new SaveFileDialog();
dlg.Title = "Open";
dlg.Filter = "Text Documents(*.*)|*.*|All Files(*.*)|*.*";
dlg.ShowDialog();
richTextBox1.SaveFile(dlg.FileName);
}
推荐答案
这表明文件名变量引用的是空字符串.
加载文件之前,您应该进行更多检查:
That suggests that the filename variable refers to an empty string.
You should include more checks before loading file:
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = "Open";
dlg.Filter = "Text Documents(*.*)|*.*|All Files(*.*)|*.*";
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if (!string.IsNullOrEmpty(dlg.FileName))
{
richTextBox1.LoadFile(dlg.FileName);
}
}
始终检查返回值:
Always check the return value:
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = "Open";
dlg.Filter = "Text Documents(*.*)|*.*|All Files(*.*)|*.*";
if (dlg.ShowDialog() == DialogResult.OK)
{
richTextBox1.LoadFile(dlg.FileName);
}
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog dlg = new SaveFileDialog();
dlg.Title = "Open";
dlg.Filter = "Text Documents(*.*)|*.*|All Files(*.*)|*.*";
if (dlg.ShowDialog() == DialogResult.OK)
{
richTextBox1.SaveFile(dlg.FileName);
}
}
这篇关于空路径名不合法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文