机器上有很多网络适配器,需要找到一个用于 Windows 中流量的网络适配器(来自 .net)
我的应用程序使用 Web 服务。我可以控制请求的工作站以及为此发送 MAC 地址的所有方法的参数。但后来我开始实际测试应用程序,我发现工作站有许多网络适配器 - 以太网、无线、蓝牙。当我使用下一个代码获取 MAC 地址时:
var networkAdapters = NetworkInterface.GetAllNetworkInterfaces();
if (networkAdapters == null || networkAdapters.Length == 0)
return string.Empty;
string address = string.Empty;
foreach (var adapter in networkAdapters)
{
var a = adapter.GetPhysicalAddress();
if (a != null && a.ToString() != string.Empty)
{
address = a.ToString();
break;
}
}
return address;
有时 Web 服务从工作站接收不同的 MAC 地址,但我希望始终只获取一个 MAC 地址。请帮我。
My application use Web-service. I'm control from what workstation was request and for this send MAC-Address how parameter of all methods. But then I start testing application in real, I found workstations which have many network adapters - Ethernet, Wireless, Bluetooth. When I get MAC-address using next code:
var networkAdapters = NetworkInterface.GetAllNetworkInterfaces();
if (networkAdapters == null || networkAdapters.Length == 0)
return string.Empty;
string address = string.Empty;
foreach (var adapter in networkAdapters)
{
var a = adapter.GetPhysicalAddress();
if (a != null && a.ToString() != string.Empty)
{
address = a.ToString();
break;
}
}
return address;
Sometimes Web-service receive from workstation different MAC-Addresses, but I want get always only one MAC-address. Please, help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我有两个不同的以太网卡 - 两个不同的 MAC 地址 - 哪个 MAC 地址是正确的地址?
回答这个问题就是回答这个问题的根本。您必须创建算法来选择正确的选择 - 有很多工具可以让您更具辨别力:
GetIsNetworkAvailable
==true
- 仅识别具有活动连接的接口速度
- 使您能够探测最快连接NetworkInterfaceType
例如以太网即使给出这些规则,多个适配器仍然可以匹配。例如,具有多个绑定到同一 LAN 的 NIC 的计算机。只有您才能确定决胜局的规则。
If I had two different ethernet cards - two different MAC addresses - which MAC address would be the right address?
Answering that is the root of answering this question. You have to create the algorithm to pick the right choice - there are lots of tools at hand to enable you to be more discriminating:
GetIsNetworkAvailable
==true
- identifies only interfaces with active connectionsSpeed
- enables you to probe for fastest connectionNetworkInterfaceType
e.g. EthernetEven given these rules, multiple adapters may still match. e.g. a machine with multiple NICs bound to the same LAN. Only you can determine rules for tiebreakers.
您知道您的应用程序使用哪个 IP 地址来提供 Web 服务,对吧?恰好有一个适配器具有此 IP 地址。
Ypou 可以通过
GetIPProperties
> 检查这一点单播地址
You know what IP address your application is using for the Web service, right? Precisely one adapter will have this IP address.
Ypou can check this via
GetIPProperties
>UnicastAddresses