Many network adapters at machine, need to find one that is used for traffic in Windows (from .net)
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.E开发者_如何学运维mpty;
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.
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. Ethernet
Even 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.
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
精彩评论