C#:如何仅检索连接到物理网卡的网络接口?
在我的C#程序中,我需要列举所有实际网卡(Wifi或以太网)的网络接口,也就是说,连接到实际物理设备的接口,而不是VPN连接等 我正在使用C#:如何仅检索连接到物理网卡的网络接口?,c#,C#,在我的C#程序中,我需要列举所有实际网卡(Wifi或以太网)的网络接口,也就是说,连接到实际物理设备的接口,而不是VPN连接等 我正在使用NetworkInterface.GetAllNetworkInterfaces()枚举网络接口,但我不知道如何筛选物理设备的网络接口…您需要使用枚举 这些文档中包含了一个很好的例子来说明这一点 public static void DisplayTypeAndAddress() { IPGlobalProperties computerProper
NetworkInterface.GetAllNetworkInterfaces()
枚举网络接口,但我不知道如何筛选物理设备的网络接口…您需要使用枚举
这些文档中包含了一个很好的例子来说明这一点
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();
}
}
您必须仔细阅读所有提供的信息,但是,该类将为您提供有关适配器配置的信息,例如ip地址等,该类提供有关每个适配器的“静态”信息(最高速度、制造商等) 前缀为“PCI”的NIs是物理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")
{
}
}
}
不,那没用。VPN连接注册为“以太网”,与普通网卡相同。听起来像是xy问题。您需要完成什么?我需要确定计算机是在1GB连接上运行还是在旧的100MB连接上运行。那么为什么不检查.Speed?NetworkInterfaceType对我没有帮助。例如,Forticlient VPN连接注册为“以太网”-就像我的实际网卡一样。使用adapter.GetIPProperties()根据具有正确网络IP设置的适配器缩小范围如何?好主意,但可能不完全正确。你也应该考虑USB适配器、PCMCIA等的情况。我爱上了这个解决方案!非常感谢你,香农!这无法识别USB适配器。我=悲伤
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")
{
}
}
}