在我的 windows 窗体中,应用程序应该提取 wifi 信号强度并将结果输出到文本框中。
它运行良好,没有错误,但是当我运行它时,我得到一个管理异常未处理错误,突出显示以下语句中的“in” :
foreach (ManagementObject mo in searcher.Get()).
我不确定如何编辑代码来解决这个问题,最终允许我在表单的文本框中显示当前的 wifi 信号强度。
代码:
class NativeWiFi
{
public string wifiStatus;
public NativeWiFi() //gets signal strength
{
ManagementObjectSearcher searcher = new
ManagementObjectSearcher(@"root\WMI", "select Ndis80211ReceivedSignalStrength
from MSNdis_80211_ReceivedSignalStrength where active=true");
StringBuilder sb = new StringBuilder();
//output as a string
foreach (ManagementObject mo in searcher.Get()) //**PROBLEM IS HERE**
{
sb.Append( mo["Ndis80211ReceivedSignalStrength"]);
}
wifiStatus = sb.ToString();
}
}
形式:
private void Form1_Load(object sender, EventArgs e) // on load, create timer
{
var timer = new Timer();
timer.Tick += new EventHandler(timer_Tick);
timer.Interval = 2000; //2 seconds
timer.Start();
}
//after 2 seconds, update the text box called 'wifi' to output current wifi strength
void timer_Tick(object sender, EventArgs e)
{
wifi.Text = signal.wifiStatus;
}
有什么想法可以让这些一起工作吗?谢谢。