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

偶尔善良 提交于 2020-01-24 17:00:28

问题


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.


回答1:


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



回答2:


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




回答3:


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


来源:https://stackoverflow.com/questions/39346232/how-to-get-currently-connected-wifi-ssid-in-c-sharp-using-wmi-or-system-net-netw

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!