因此,当我按列表框中的空格时,会出现此错误
System.IO.DirectoryNotFoundException:'找不到路径'C:\ Users \ tamov \ Downloads \ PEOROX EWEEQWWW \ scripts \
的一部分
这是我的代码
string script = File.ReadAllText(string.Format("./scripts/{0}", this.listBox1.SelectedItem));
bool flag = this.listBox1.SelectedItem != null;
bool flag2 = flag;
if (flag2)
{
this.webBrowser1.Document.InvokeScript("SetText", new object[]
{
script
});
}
答案 0 :(得分:0)
如果您不选择任何内容,则您的SelectedItem可以为null,如注释中正确指出的那样。
发布的代码似乎在检查SelectedItem之前就已经访问过了它。
将其拉入if条件中,以确保仅当SelectedItem不为null时才执行它。
bool flag = this.listBox1.SelectedItem != null;
bool flag2 = flag;
if (flag2)
{
string script = File.ReadAllText(string.Format("./scripts/{0}", this.listBox1.SelectedItem));
this.webBrowser1.Document.InvokeScript("SetText", new object[]
{
script
});
}
或者,如果在script
条件之外的其他位置需要变量if
,则在原始位置之外声明变量,但在if
内部设置变量。
答案 1 :(得分:0)
我希望看到这样写:
if (this.listBox1.SelectedIndex != -1)
{
string script = File.ReadAllText(string.Format("./scripts/{0}", this.listBox1.SelectedItem));
this.webBrowser1.Document.InvokeScript("SetText", new object[] { script });
}