27

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.

mariosangiorgio
  • 1,007
  • 2
  • 13
  • 26

7 Answers7

26

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)));
}
Marcello B.
  • 4,177
  • 11
  • 45
  • 65
mariosangiorgio
  • 1,007
  • 2
  • 13
  • 26
  • 1
    This does not work. It requires including an assembly that I did not manage to find! Can you point me to the right direction? – MrAsterisco Sep 27 '13 at 12:22
  • 5
    @MrAsterisco: You need the Managed WiFi (http://www.codeplex.com/managedwifi) library mentioned above. – habakuk Oct 21 '13 at 14:54
  • WlanClient not found. – huang May 14 '21 at 17:04
  • The library I used to make this code work is called SimpleWifi, available here: [NuGet](https://www.nuget.org/packages/SimpleWifi) / [GitHub](https://github.com/DigiExam/simplewifi) – JamesP Feb 16 '23 at 14:19
9

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;
Marcello B.
  • 4,177
  • 11
  • 45
  • 65
Byron Ross
  • 1,595
  • 1
  • 14
  • 21
3

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

ine
  • 14,014
  • 8
  • 55
  • 80
2

there is some more information in How do I get the available wifi APs and their signal strength in .net?

Community
  • 1
  • 1
LDomagala
  • 2,193
  • 4
  • 20
  • 34
1

(cross-posted in How to get currently connected wifi SSID in c# using WMI or System.Net.NetworkInformation windows 10?)

I found a rather old library dating back to 2014:

Microsoft.WindowsAPICodePack-Core version 1.1.0.2

Although it is not conforming to .NET Standard, this library integrates with my .NET Core 3.0 app, but obviously is not cross-platform.

Sample code:

var networks = NetworkListManager.GetNetworks(NetworkConnectivityLevels.Connected);            
foreach (var network in networks) { 
    sConnected = ((network.IsConnected == true) ? " (connected)" : " (disconnected)");
    Console.WriteLine("Network : " + network.Name + " - Category : " + network.Category.ToString() + sConnected);
}
Carl in 't Veld
  • 1,363
  • 2
  • 14
  • 29
0

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.

Recep
  • 18,991
  • 2
  • 28
  • 21
0

I wanted to do exactly this, and tried using ManagedWifi, as suggested in other answers. But that led to unresolvable Exceptions as per here: Issues with using Managed WiFi (NativeWiFi API)

I solved this by switching to using SimpleWiFi entirely and ignored the ManagedWifi package.

Glancing at the source code, it looks like SW is a fixed reimplementation of some of the functionality in MW.

Brondahl
  • 7,402
  • 5
  • 45
  • 74