如何使用WMI或System.Net.NetworkInformation窗口10在c#中获取当前连接的wifi SSID?

时间:2016-09-06 10:04:50

标签: c# wmi ssid

我尝试了两种不同的方法

第一种方式:null异常问题

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){

}

第二种方式:我选择了Wi-Fi,但无法获得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 + ")";
        }
    }
}

请问有人能让我清楚地知道如何获得我的连接wifi SSID。

4 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

您可以尝试使用下面提到的WMI课程。两者都在 cimv2

中定义

SELECT * FROM WiFi_AdapterAssociationInfo

<强> SELECT * FROM WiFi_AvailableNetwork

有关详情:WIFI Information

答案 2 :(得分:0)

我发现了一个可以追溯到2014年的老图书馆:

Microsoft.WindowsAPICodePack-Core version 1.1.0.2

尽管它不符合.NET标准,但该库与我的.NET Core 3.0应用程序集成在一起,但显然不是跨平台的。

示例代码:

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

答案 3 :(得分:0)

我想做到这一点。 有一个nuget程序包似乎包装了较低级别的API,称为SimpleWifi,对我来说非常合适。

请注意,这似乎是早期库“ ManagedWifi”的错误修复重新实现,如此处所示,该库对我抛出了无法解决的异常: Issues with using Managed WiFi (NativeWiFi API)