OpenFileDialog错误:'路径不合法“

时间:2018-03-04 13:18:15

标签: c# winforms openfiledialog

代码如下:

OpenFileDialog fDialog = new OpenFileDialog();
fDialog.Title = "Select script to load...";
fDialog.Filter = "Text Files|*.txt";

fDialog.ShowDialog();

string text = File.ReadAllText(Path.GetFullPath(fDialog.FileName));
if (text.Length > 0)
{
    LuaCArea.Text = text;
} 

我收到错误:

  

路径不是合法形式。

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

等待OpenFileDialog结果dud,不要使用Path.GetFullPath

OpenFileDialog fDialog = new OpenFileDialog();
fDialog.Title = "Select script to load...";
fDialog.Filter = "Text Files|*.txt";

if(fDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
    LuaCArea.Text = File.ReadAllText(fDialog.FileName);
}