名称VARname在当前上下文C#中不存在

问题描述 投票:0回答:1

我知道有很多类似的问题/答案,但我无法根据自己的问题来调整解决方案:

我试图进行迭代,遍历列表框的项目(所有元素都是路径),我想通过单击按钮,使用默认的Windows程序将其全部打开。任何帮助将不胜感激。

enter image description here

c# listbox visual-studio-2019
1个回答
4
投票

问题是foreach语句后的分号。分号立即结束foreach语句。您上面的代码等效于:

foreach (string myitem in this.listBox1.Items)
{
  // myitem is only available in this scope
}

MessageBox.Show(myitem.toString(), "My Caption", MessageBoxButtons.OK, MessageBoxIcon.Information);
System.Diagnostics.Process.Start(myitem.ToString());

您需要将所需的所有内容封装在这样的范围内的foreach中:

foreach (string myitem in this.listBox1.Items)
{
  MessageBox.Show(myitem.toString(), "My Caption", MessageBoxButtons.OK, MessageBoxIcon.Information);
  System.Diagnostics.Process.Start(myitem.ToString());
}
© www.soinside.com 2019 - 2024. All rights reserved.