How to get currently connected wifi SSID in c# using WMI or System.Net.NetworkInformation windows 10?
Asked Answered
F

4

3

I have tried in 2 different ways

First way: null exception issue

try{

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

}catch(Exception ex){

}

Second way: I'm gating wi-fi but couldnt get the SSID

if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()) {
    foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces()) {

   if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 && ni.OperationalStatus== OperationalStatus.Up   ) {
       Network = "NETWORK ( N/A )";
           Wifi = "Wifi (" + ni.Name + ")";
        }
    }
}

Could you please someone give me clear idea how to get my connected wifi SSID.

Factor answered 6/9, 2016 at 10:4 Comment(0)
Z
5
    // Show SSID and Signal Strength
    private void showConnectedId() {
        System.Diagnostics.Process p = new System.Diagnostics.Process();
        p.StartInfo.FileName = "netsh.exe";
        p.StartInfo.Arguments = "wlan show interfaces";
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        p.Start();

        string s = p.StandardOutput.ReadToEnd();
        string s1 = s.Substring(s.IndexOf("SSID"));
        s1 = s1.Substring(s1.IndexOf(":"));
        s1 = s1.Substring(2, s1.IndexOf("\n")).Trim();

        string s2 = s.Substring(s.IndexOf("Signal"));
        s2 = s2.Substring(s2.IndexOf(":"));
        s2 = s2.Substring(2, s2.IndexOf("\n")).Trim();

        labelStatus.Text = "WIFI connected to " + s1 + "  " + s2;
        p.WaitForExit();
    }
Zarah answered 3/1, 2017 at 21:47 Comment(2)
Can I use this in UWP?Holm
Looks like you cant run netsh from UWP. Just a heads up.Holm
G
0

You can try using below mentioned WMI classes. Both are defined in cimv2

SELECT * FROM WiFi_AdapterAssociationInfo

SELECT * FROM WiFi_AvailableNetwork

For more details:WIFI Information

Garzon answered 7/9, 2016 at 18:19 Comment(6)
Hi, Thanks for your answer and reference WIFI InformationFactor
If that fixes your problem, please considering marking the answer as up vote. If not, please share further issues.Garzon
WMI are not supported in UWP. social.msdn.microsoft.com/Forums/sqlserver/en-US/… Just a heads up.Holm
This is not working at my machine. ([wmisearcher]”SELECT * FROM WiFi_AvailableNetwork”).get() results in: An error occurred while enumerating through a collection: Invalid class .Ramshackle
Same on mine (W10x64): invalid class.Georama
can you provide information about your environment? hardware and windows info.Garzon
I
0

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);
}
Ishmul answered 13/11, 2019 at 20:15 Comment(0)
C
0

I wanted to do exactly this. There's a nuget package that appears to wrap the lower level APIs, called SimpleWifi which worked perfectly for me.

Note that it seems to be a bug-fixing reimplementation of an earlier library: "ManagedWifi", which was throwing unresolvable Exceptions for me, as per here: Issues with using Managed WiFi (NativeWiFi API)

Castor answered 7/2, 2020 at 14:51 Comment(1)
SimpleWifi certainly works but it has not been updated since 2015. I've succesfully used a more recent nuget package called ManagedNativeWifi github.com/emoacht/ManagedNativeWifiKistler

© 2022 - 2024 — McMap. All rights reserved.