Reputation: 2244
In my C# program, I need to enumerate all network interfaces which are actual network cards (Wifi or Ethernet), meaning - the ones connected to actual physical devices, as opposed to VPN connections, etc.
I'm using NetworkInterface.GetAllNetworkInterfaces()
to enumerate NetworkInterfaces, but I don't know how to filter those for physical devices...
Upvotes: 3
Views: 3382
Reputation: 430
The NIs with prefix "PCI" are physical NIs.
NetworkInterface[] fNetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in fNetworkInterfaces)
{
string fRegistryKey = "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\" + adapter.Id + "\\Connection";
RegistryKey rk = Registry.LocalMachine.OpenSubKey(fRegistryKey, false);
if (rk != null)
{
string fPnpInstanceID = rk.GetValue("PnpInstanceID", "").ToString();
int fMediaSubType = Convert.ToInt32(rk.GetValue("MediaSubType", 0));
if (fPnpInstanceID.Length > 3 && fPnpInstanceID.Substring(0, 3) == "PCI")
{
}
}
}
Upvotes: 3
Reputation: 12369
You will have to dig though all the provided info, however, the Win32_NetworkAdapterConfiguration class will give you info about the configuration of your adapters e.g. ip addresses etc and the Win32_NetworkAdapter class provides 'static' information about each adapter (max speed, manufacturer etc).
string query = "SELECT * FROM Win32_NetworkAdapterConfiguration";
ManagementObjectSearcher moSearch = new ManagementObjectSearcher(query);
ManagementObjectCollection moCollection = moSearch.Get();// Every record in this collection is a network interface
foreach (ManagementObject mo in moCollection)
{
// ....
}
Upvotes: 0
Reputation: 12369
You need to use the NetworkInterfaceType Enumeration.
The docs include a pretty decent example for figuring this out.
public static void DisplayTypeAndAddress()
{
IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
Console.WriteLine("Interface information for {0}.{1} ",
computerProperties.HostName, computerProperties.DomainName);
foreach (NetworkInterface adapter in nics)
{
IPInterfaceProperties properties = adapter.GetIPProperties();
Console.WriteLine(adapter.Description);
Console.WriteLine(String.Empty.PadLeft(adapter.Description.Length,'='));
Console.WriteLine(" Interface type .......................... : {0}", adapter.NetworkInterfaceType);
Console.WriteLine(" Physical Address ........................ : {0}",
adapter.GetPhysicalAddress().ToString());
Console.WriteLine(" Minimum Speed............................ : {0}", adapter.Speed);
Console.WriteLine(" Is receive only.......................... : {0}", adapter.IsReceiveOnly);
Console.WriteLine(" Multicast................................ : {0}", adapter.SupportsMulticast);
Console.WriteLine();
}
}
Upvotes: 0