c# - 当前上下文 C# 中不存在名称 VARname
问题描述
我知道有很多类似的问题/答案,但我无法根据我的问题调整解决方案:
我正在尝试迭代,循环遍历列表框的项目(所有元素都是路径),我想通过单击按钮使用默认的 Windows 程序打开它们。任何帮助将非常感激。
解决方案
问题是语句后面的分号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());
}
推荐阅读
- typescript - 如何声明具有可变数量属性的 TypeScript 接口?
- python - 如何在特定索引后将数字附加到 txt 文件
- unity3d - AR Foundation 4.0.2 使用 Unity2019.3 更新问题 TestCameraImage
- azure - Azure IoT 中心 - 如果在 IoT 中心设置了路由,则 IoTHubTrigger Azure 函数不会执行
- c - 在 C 中调用 abort() 时如何指定消息?
- google-bigquery - 使用 gmail 日志和 BQ 审核 SMTP 中继中的条目
- python - 我可以在转换为 dict 时省略的数据类中的可选参数吗?
- python - 预期输入 batch_size (32) 与目标 batch_size (19840) BERT 分类器匹配
- cassandra - Cassandra 环境设置中的 replace_address first_boot 与 replace_address 有什么不同?
- c# - C# 将 Windows 窗体设置在前面但不给它焦点