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.

4 Answers4

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

Amit Shakya
  • 1,396
  • 12
  • 27
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);
}
Carl in 't Veld
  • 1,363
  • 2
  • 14
  • 29
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)

Brondahl
  • 7,402
  • 5
  • 45
  • 74
  • SimpleWifi certainly works but it has not been updated since 2015. I've succesfully used a more recent nuget package called ManagedNativeWifi https://github.com/emoacht/ManagedNativeWifi – fvlinden Apr 27 '21 at 07:10