问题
I want to differentiate between the Ethernets available on my system.
Whether that's a LAN , Wifi or something created by VMWare. I am only intrested for LAN.
But when I am enumerating with following code:
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface networkInterface in adapters)
{
Console.WriteLine(" {0} ::: Interface Type {1} ::::ID {2} \n", networkInterface.Name, networkInterface.NetworkInterfaceType, networkInterface.Id);
}
Now how do I differentiate as NetworkInterface.Description and NetworkInterface.NetworkInterfaceType are not useful.
Any other ways around?
回答1:
Look at using WMI to retrieve more detailed information about network adapters on the system. It'll tell you whether or not a given adapter is a wired network adapter or wireless, and if it is indeed a 'physical' network adapter.
http://geekswithblogs.net/PsychoCoder/archive/2008/01/25/using_wmi_in_csharp.aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/aa394216(v=vs.85).aspx
回答2:
To find out if the adapter is a Wifi adapter you can use the MSNdis_PhysicalMediumType class in the root\WMI namespace.
Unfortunately the VMware adapters appear to Windows as if they are "real" physical adapters and have a MAC address and IP address etc so it's hard to differentiate those.
You'll notice in Windows if you run ncpa.cpl they all appear the same as physical adapters.
来源:https://stackoverflow.com/questions/11753757/how-to-identify-if-the-network-interface-is-lan-or-wifi-or-external