C#:如何仅检索连接到物理网络卡的网络接口?

3
在我的C#程序中,我需要枚举所有实际网络接口(Wifi或以太网),也就是连接到实际物理设备的接口,而不是VPN连接等等。
我正在使用NetworkInterface.GetAllNetworkInterfaces()来枚举NetworkInterfaces,但我不知道如何过滤出那些物理设备...

https://msdn.microsoft.com/zh-cn/library/system.net.networkinformation.networkinterface.networkinterfacetype(v=vs.110).aspx - undefined
不,这并没有帮助。VPN连接被注册为“以太网”,与普通的网络适配器一样。 - undefined
听起来像是一个xy问题。你需要完成什么任务? - undefined
我需要弄清楚计算机是运行在1GB连接还是较旧的100MB连接上。 - undefined
那为什么不直接检查.Speed呢? - undefined
请参考此链接https://dev59.com/iXA75IYBdhLWcg3w3NHf#65090230,以获取仅物理适配器。 - undefined
3个回答

2

以“PCI”为前缀的网络接口(NI)是物理网络接口。


            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")
                    {

                    }
                }
            }

1
好主意,但可能不完全正确。你还应考虑到USB适配器、PCMCIA等情况。 - undefined
我爱上了这个解决方案!非常感谢你,@Shannon! - undefined

0
你需要使用NetworkInterfaceType枚举。
文档中包含了一个相当不错的示例,可以帮助你弄清楚这个问题。
 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();
      }
   }

NetworkInterfaceType对我没有帮助。例如,Forticlient VPN连接被注册为“以太网” - 就像我的实际网络适配器一样。 - undefined
使用adapter.GetIPProperties()来根据具有正确网络IP设置的适配器进行筛选,这个怎么样? - undefined

0

你将不得不仔细查看所有提供的信息,然而,Win32_NetworkAdapterConfiguration 类将为您提供关于适配器配置的信息,例如 IP 地址等,而 Win32_NetworkAdapter 类则提供有关每个适配器的“静态”信息(最大速度、制造商等)。

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)
{
    // ....
}

这个不识别USB适配器。我很伤心。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接