Search code examples
c#textboxwmiwifi

Management Object Exception error on form that displays WiFi signal strength


In my windows form, the application is supposed to extract the wifi signal strength and output the result into a text box.

It complies fine with no errors, but when I run it, I get a Management Exception Unhandled error, highlighting the 'in' in the following statement :

foreach (ManagementObject mo in searcher.Get()).

I am not sure how to edit the code to fix this problem, ultimately allowing me to display the current wifi signal strength in a text box on my form.

Code:

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();  
    }

}   

FORM:

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;
}

Any ideas how I can get these to work together? Thanks.


Solution

  • I am also troubled by some problem about WMI. I found that WMI function can not get Ndis80211ReceivedSignalStrength information at Windows *7* and Windows 8. I tried to use the WLAN SDK get the WIFI signal Quality successfully. You can refer to the below URL. http://msdn.microsoft.com/en-us/library/windows/desktop/ms706765%28v=vs.85%29.aspx