获取我在 Windows Vista 上使用 C# .Net 连接到的无线网络的 SSID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/431755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 03:08:19  来源:igfitidea点击:

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

c#windows-vistawireless

提问by mariosangiorgio

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)

我想知道是否有任何 .Net 类可以让我知道我连接到的无线网络的 SSID。到目前为止,我只找到了下面链接的库。是我能得到的最好的还是应该使用其他的? 托管 WiFi( http://www.codeplex.com/managedwifi)

The method that exploits WMIworks for Windows XP but is it not working anymore with Windows Vista.

利用WMI的方法适用于 Windows XP,但它不再适用于 Windows Vista。

采纳答案by mariosangiorgio

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

首先我必须创建一个 WlanClient 对象

wlan = new WlanClient();

And then I can get the list of the SSIDs the PC is connected to with this code:

然后我可以使用以下代码获取 PC 连接到的 SSID 列表:

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

回答by ine

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

来自http://bytes.com/groups/net-c/657473-wmi-wifi-discovery

回答by Recep

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.

您将不得不使用本机 WLAN API。有一个关于它的长时间的讨论在这里。显然这是 Managed Wifi API 所使用的,因此如果您对使用 LGPL 代码没有任何限制,那么使用它会更容易。

回答by Byron Ross

We were using the managed wifi library, but it throws exceptions if the network is disconnected during a query.

我们使用的是托管 wifi 库,但如果在查询期间网络断开,它会抛出异常。

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;

回答by Carl in 't Veld

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

(交叉发布在如何使用 WMI 或 System.Net.NetworkInformation windows 10 在 c# 中获取当前连接的 wifi SSID?

I found a rather old library dating back to 2014:

我发现了一个可以追溯到 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.

虽然它不符合 .NET Standard,但该库与我的 .NET Core 3.0 应用程序集成,但显然不是跨平台的。

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

回答by Brondahl

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)

我想完全做到这一点,并按照其他答案中的建议尝试使用 ManagedWifi。但这导致了无法解决的异常,如下所示: Issues with using Managed WiFi (NativeWiFi API)

I solved this by switching to using SimpleWiFientirely and ignored the ManagedWifi package.

我通过完全切换到使用SimpleWiFi解决了这个问题,并忽略了 ManagedWifi 包。

Glancing at the source code, it looks like SW is a fixed reimplementation of some of the functionality in MW.

看一下源代码,看起来 SW 是 MW 中某些功能的固定重新实现。