【问题标题】:C#: How do I retrieve only network interfaces attached to physical network cards?C#:如何仅检索连接到物理网卡的网络接口?
【发布时间】:2018-06-21 17:32:10
【问题描述】:

在我的 C# 程序中,我需要枚举所有作为实际网卡(Wifi 或以太网)的网络接口,这意味着 - 连接到实际物理设备的网络接口,而不是 VPN 连接等。

我正在使用NetworkInterface.GetAllNetworkInterfaces() 枚举网络接口,但我不知道如何为物理设备过滤这些...

【问题讨论】:

  • 不,这没有帮助。 VPN 连接注册为“以太网”,与普通网卡相同。
  • 听起来像是 xy 问题。你需要完成什么?
  • 我需要确定计算机运行的是 1GB 连接还是旧的 100MB 连接。
  • 那为什么不检查一下.Speed呢?

标签: c#


【解决方案1】:

前缀为“PCI”的 NI 是物理 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")
                    {

                    }
                }
            }

【讨论】:

  • 好主意,但可能不完全正确。还要考虑USB适配器、PCMCIA等的情况。
  • 我爱上了这个解决方案!非常感谢你,@香农!
【解决方案2】:

您需要使用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 连接注册为“以太网”——就像我的实际网卡一样。
  • 如何使用 adapter.GetIPProperties() 根据具有正确网络 IP 设置的适配器来缩小范围?
【解决方案3】:

您将不得不挖掘所有提供的信息,但是,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 适配器。我=伤心
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-22
  • 1970-01-01
  • 2010-10-05
  • 2018-12-24
  • 1970-01-01
相关资源
最近更新 更多