从 C# 获取无线接入点的 BSSID(MAC 地址)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/187777/
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
Get BSSID (MAC address) of wireless access point from C#
提问by Iain
How can I get the BSSID / MAC (Media Access Control) address of the wireless access point my system is connected to using C#?
如何使用 C# 获取系统连接到的无线接入点的 BSSID/MAC(媒体访问控制)地址?
Note that I'm interested in the BSSID of the WAP. This is different from the MAC address of the networking portion of the WAP.
请注意,我对 WAP 的 BSSID 感兴趣。这与 WAP 网络部分的 MAC 地址不同。
采纳答案by Iain
The following needs to be executed programmatically:
以下需要以编程方式执行:
netsh wlan show networks mode=Bssid | findstr "BSSID"
The above shows the access point's wireless MAC addresses which is different from:
上面显示了接入点的无线 MAC 地址,它不同于:
arp -a | findstr 192.168.1.254
This is because the access point has 2 MAC addresses. One for the wireless device and one for the networking device. I want the wireless MAC but get the networking MAC using arp.
这是因为接入点有 2 个 MAC 地址。一种用于无线设备,一种用于网络设备。我想要无线 MAC,但使用arp获取网络 MAC 。
Using the Managed Wifi API:
使用托管 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);
}
}
回答by James Curran
回答by Tim Farley
About getting that result from ARP.EXE programmatically:
关于以编程方式从 ARP.EXE 获取该结果:
The Win32 API to get this is in the IP Helpergroup of functions and it is called GetIpNetTable(). The P/Invoke signature for it is here. You'll have to write some code to marshal the results out of it, and its one of those fun Win32 APIs with variable length results.
用于获取此信息的 Win32 API 位于IP Helper 函数组中,它称为GetIpNetTable()。它的P/Invoke 签名在这里。您必须编写一些代码来将结果编组出来,它是具有可变长度结果的有趣的 Win32 API 之一。
Another way to do this would be to use Windows Management Instrumentationwhich does have a nice set of wrapper classes in the System.Management and System.Management.Instrumentation namespaces. But the down side is the WMI service must be running for that to work. I've dug around but I can't seem to find the exact object in the WMI tree that contains the equivalent information. I'm pretty sure it exists because I see third-party tools on the net that claim to retrieve this info using this API. Maybe someone else will chime in with that part.
另一种方法是使用Windows Management Instrumentation,它在System.Management 和 System.Management.Instrumentation 命名空间中有一组很好的包装类。但不利的一面是 WMI 服务必须运行才能正常工作。我四处寻找,但似乎无法在 WMI 树中找到包含等效信息的确切对象。我很确定它存在,因为我在网上看到第三方工具声称可以使用此 API 检索此信息。也许其他人会加入那部分。
回答by Lennard
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);
}
}
Beware of syntax error like curly braces all that. But the concept is here. You may create Scan function by periodically invoking this process. Correct me if something goes wrong.
小心像花括号这样的语法错误。但概念就在这里。您可以通过定期调用此过程来创建扫描功能。如果出现问题,请纠正我。