Question

I'd like to know if there is any .Net class that allows me to know the SSID of the wireless network I'm connected to. So far I only found the library linked below. Is the best I can get or should I use something else? Managed WiFi (http://www.codeplex.com/managedwifi)

The method that exploits WMI works for Windows XP but is it not working anymore with Windows Vista.

Was it helpful?

Solution

I resolved using the library. It resulted to be quite easy to work with the classes provided:

First I had to create a WlanClient object

wlan = new WlanClient();

And then I can get the list of the SSIDs the PC is connected to with this code:

Collection<String> connectedSsids = new Collection<string>();

foreach (WlanClient.WlanInterface wlanInterface in wlan.Interfaces)
{
   Wlan.Dot11Ssid ssid = wlanInterface.CurrentConnection.wlanAssociationAttributes.dot11Ssid;
   connectedSsids.Add(new String(Encoding.ASCII.GetChars(ssid.SSID,0, (int)ssid.SSIDLength)));
}

OTHER TIPS

We were using the managed wifi library, but it throws exceptions if the network is disconnected during a query.

Try:

var process = new Process
{
    StartInfo =
    {
    FileName = "netsh.exe",
    Arguments = "wlan show interfaces",
    UseShellExecute = false,
    RedirectStandardOutput = true,
    CreateNoWindow = true
    }
};
process.Start();

var output = process.StandardOutput.ReadToEnd();
var line = output.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).FirstOrDefault(l => l.Contains("SSID") && !l.Contains("BSSID"));
if (line == null)
{
    return string.Empty;
}
var ssid = line.Split(new[] { ":" }, StringSplitOptions.RemoveEmptyEntries)[1].TrimStart();
return ssid;

It looks like this will do what you want:

ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\WMI",
"SELECT * FROM MSNdis_80211_ServiceSetIdentifier");


foreach (ManagementObject queryObj in searcher.Get())
{
    Console.WriteLine("-----------------------------------");
    Console.WriteLine("MSNdis_80211_ServiceSetIdentifier instance");
    Console.WriteLine("-----------------------------------");

    if(queryObj["Ndis80211SsId"] == null)
        Console.WriteLine("Ndis80211SsId: {0}",queryObj["Ndis80211SsId"]);
    else
    {
        Byte[] arrNdis80211SsId = (Byte[])
        (queryObj["Ndis80211SsId"]);
        foreach (Byte arrValue in arrNdis80211SsId)
        {
            Console.WriteLine("Ndis80211SsId: {0}", arrValue);
        }
    }
}

from http://bytes.com/groups/net-c/657473-wmi-wifi-discovery

You are going to have to use native WLAN API. There is a long discussion about it here. Apparently this is what Managed Wifi API uses, so it will be easier for you to use it if you do not have any restrictions to use LGPL code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top