Get SSID of the wireless network I am connected to with C# .Net on Windows Vista
Asked Answered
F

8

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.

Fiord answered 10/1, 2009 at 21:5 Comment(0)
F
27

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)));
}
Fiord answered 10/1, 2009 at 23:35 Comment(4)
This does not work. It requires including an assembly that I did not manage to find! Can you point me to the right direction?Dubonnet
@MrAsterisco: You need the Managed WiFi (codeplex.com/managedwifi) library mentioned above.Pocketbook
WlanClient not found.Phellem
The library I used to make this code work is called SimpleWifi, available here: NuGet / GitHubGrays
M
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;
Montpelier answered 23/8, 2014 at 9:37 Comment(1)
This may not work in all Windows languages, as the output of netsh is localized.Tauromachy
R
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

Rosaliarosalie answered 10/1, 2009 at 21:12 Comment(1)
Thanks for the answer. Unfortunately this method is not working with Windows Vista. Do you have any other idea?Fiord
R
2

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

Rios answered 2/2, 2009 at 21:46 Comment(0)
P
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);
}
Plasticizer answered 13/11, 2019 at 20:20 Comment(0)
D
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.

Delegacy answered 10/1, 2009 at 23:10 Comment(1)
That is what I did. It revealed to be easy. Thanks for your answer.Fiord
F
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.

Fabulist answered 7/2, 2020 at 14:48 Comment(0)
A
0

I realized this using the WLAN API of Windows with this code:

WlanOpenHandle(2, 0, out var _, out var clientHandle);

WlanQueryInterface(
    clientHandle,
    Guid.Parse(networkInterface.Id),
    wlan_intf_opcode_current_connection,
    0,
    out var _,
    out var data,
    out var _);

var connectionAttributes = Marshal.PtrToStructure<WLAN_CONNECTION_ATTRIBUTES>(data);

var SSID = connectionAttributes.wlanAssociationAttributes.dot11Ssid.ucSSID;

WlanFreeMemory(data);

WlanCloseHandle(clientHandle);

To use this code, you need the networkInterface, which you can get with NetworkInterface.GetAllNetworkInterfaces and the result will be stored in the SSID variable.

Aegisthus answered 24/6 at 5:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.