从C#获取无线接入点的BSSID(MAC地址)

如何使用C#获取我的系统连接到的无线接入点的BSSID / MAC(媒体访问控制)地址?

请注意,我对WAP的BSSID感兴趣。 这与WAP的网络部分的MAC地址不同。

以下需要以编程方式执行:

netsh wlan show networks mode=Bssid | findstr "BSSID" 

以上显示了接入点的无线MAC地址,它与以下内容不同:

 arp -a | findstr 192.168.1.254 

这是因为接入点有2个MAC地址。 一个用于无线设备,一个用于网络设备。 我想要无线MAC,但使用arp获取网络MAC。

使用托管Wifi API :

 var wlanClient = new WlanClient(); foreach (WlanClient.WlanInterface wlanInterface in wlanClient.Interfaces) { Wlan.WlanBssEntry[] wlanBssEntries = wlanInterface.GetNetworkBssList(); foreach (Wlan.WlanBssEntry wlanBssEntry in wlanBssEntries) { byte[] macAddr = wlanBssEntry.dot11Bssid; var macAddrLen = (uint) macAddr.Length; var str = new string[(int) macAddrLen]; for (int i = 0; i < macAddrLen; i++) { str[i] = macAddr[i].ToString("x2"); } string mac = string.Join("", str); Console.WriteLine(mac); } } 

这个问题告诉我们如何从网络连接中获取您想要的任何信息。 (使用NetworkInformation向下滚动到答案)

 using System; using System.Diagnostics; class Program { static void Main(string[] args) { Process proc = new Process(); proc.StartInfo.CreateNoWindow = true; proc.StartInfo.FileName = "cmd"; proc.StartInfo.Arguments = @"/C ""netsh wlan show networks mode=bssid | findstr BSSID """; proc.StartInfo.RedirectStandardOutput = true; proc.StartInfo.UseShellExecute = false; proc.Start(); string output = proc.StandardOutput.ReadToEnd(); proc.WaitForExit(); Console.WriteLine(output); } } 

请注意语法错误,如花括号。 但这个概念就在这里。 您可以通过定期调用此过程来创建扫描function。 如果出现问题,纠正我。

关于以编程方式从ARP.EXE获取该结果:

用于获取此function的Win32 API位于IP Helperfunction组中,称为GetIpNetTable() 。 它的P / Invoke签名就在这里 。 您将不得不编写一些代码来编组结果,并且它是具有可变长度结果的那些有趣的Win32 API之一。

另一种方法是使用Windows Management Instrumentation ,它在System.Management和System.Management.Instrumentation命名空间中有一组很好的包装类。 但缺点是WMI服务必须运行才能工作。 我已经挖了但是我似乎无法在WMI树中找到包含等效信息的确切对象。 我很确定它存在,因为我在网上看到声称使用此API检索此信息的第三方工具。 也许其他人会对这部分感兴趣。