代码之家  ›  专栏  ›  技术社区  ›  Mauro

如何获取默认NIC连接名称

  •  4
  • Mauro  · 技术社区  · 14 年前

    回到这个话题上。

    有我的物理接口的MAC地址,有没有一种编程的方法来获取接口名称/接口状态等。。。

    Device Realtek RTL8139系列PCI快速以太网NIC

    XXXX-XXXX-XXXX是我所知道的

    通过这个设备,我连接使用“本地连接”连接(与网关,子网等相关的所有信息)

    所以我在搜索XXXX-XXXX-XXXX和本地连接之间的链接。

    希望现在一切都清楚了。

    谢谢大家! 抱歉耽搁了+我投票给所有人,为了耐心!


    老问题


    大家好, 我想用netsh命令更改“本地连接”的IP地址。

    谢谢

    编辑:我不需要所有连接名称的列表,只需要默认名称。访问注册表时,我得到了列表,似乎默认值被标记为*。 不幸的是,在控制台上打印它们,我会得到10种不同的“本地连接”,比如。。。

    Local Area Connection* 13
    6TO4 Adapter
    VMware Network Adapter VMnet1
    Wireless Network Connection 2
    Reusable ISATAP Interface {483968F2-DBF9-4596-B8BE-725FAAB89F93}
    Local Area Connection* 3
    Local Area Connection* 2
    Reusable Microsoft 6To4 Adapter
    Local Area Connection* 7
    VMware Network Adapter VMnet8
    Local Area Connection* 8
    isatap.replynet.prv
    Local Area Connection* 9
    Local Area Connection* 12
    isatap.{FAA80CE0-D641-408A-83F8-5F9C394FFD76}
    Bluetooth Network Connection
    Local Area Connection* 4
    isatap.{40156BF9-6599-4912-A315-62DE5342B452}
    isatap.{7651F2F5-4888-4258-92C5-6822C506D726}
    Local Area Connection* 5
    isatap.{34F5F074-8AA7-4421-AE24-131BA2DC3458}
    Local Area Connection*
    Local Area Connection* 10
    Local Area Connection
    Local Area Connection* 6
    Wireless Network Connection
    

    等等。。。

    0 - WAN Miniport (SSTP)
    1 - WAN Miniport (IKEv2)
    2 - WAN Miniport (L2TP)
    3 - WAN Miniport (PPTP)
    4 - WAN Miniport (PPPOE)
    5 - WAN Miniport (IPv6)
    6 - WAN Miniport (Network Monitor)
    7 - Realtek RTL8168C(P)/8111C(P) Family PCI-E Gigabit Ethernet NIC (NDIS 6.20)
    8 - WAN Miniport (IP)
    9 - Microsoft ISATAP Adapter
    10 - RAS Async Adapter
    11 - Broadcom 802.11g Network Adapter
    12 - Microsoft 6to4 Adapter
    13 - VMware Virtual Ethernet Adapter for VMnet1
    14 - Microsoft ISATAP Adapter #3
    15 - VMware Virtual Ethernet Adapter for VMnet8
    16 - Microsoft ISATAP Adapter #2
    17 - Microsoft ISATAP Adapter #4
    18 - Microsoft Virtual WiFi Miniport Adapter
    19 - Microsoft ISATAP Adapter #5
    20 - Microsoft ISATAP Adapter
    22 - Bluetooth Device (Personal Area Network)
    23 - Microsoft 6to4 Adapter
    24 - Microsoft 6to4 Adapter #3
    25 - Microsoft 6to4 Adapter #2
    
    6 回复  |  直到 13 年前
        1
  •  2
  •   Greg Buehler    14 年前

    这是一种肮脏的方法,因为它可以通过合并LINQ等进行优化

    using System.Net.NetworkInformation;
    
    List<NetworkInterface> Interfaces = new List<NetworkInterface>();
    foreach (var nic in NetworkInterface.GetAllNetworkInterfaces())
    {
        if (nic.OperationalStatus == OperationalStatus.Up)
        {
            Interfaces.Add(nic);
        }
    }
    
    
    NetworkInterface result = null;
    foreach (NetworkInterface nic in Interfaces)
    {
        if (result == null)
        {
            result = nic;
        }
        else
        {
            if (nic.GetIPProperties().GetIPv4Properties() != null)
            {
                if (nic.GetIPProperties().GetIPv4Properties().Index < result.GetIPProperties().GetIPv4Properties().Index)
                    result = nic;
            }
        }
    }
    
    Console.WriteLine(result.Name);
    

    nic.GetIPProperties() nic.GetIPProperties().GetIPv4Properties()

        2
  •  5
  •   Jesse    14 年前

    正如其他人所提到的,Windows中没有“默认”NIC适配器。使用的NIC是根据目标网络(地址)和度量选择的。

    例如,如果您有两个NIC和两个不同的网络:

      10.1.10.1 - Local Area Connection (metric 20)
      10.1.50.1 - Local Area Connection 2 (metric 10)
    

    你想连接到 10.1.10.15 ,Windows将选择 Local Area Connection 10.1.50.30 ,Windows将选择 Local Area Connection 2 .

    现在,如果你尝试连接到 74.125.67.106 (google.com),Windows将选择 因为它的度量值较低。

    编辑:这里有一篇很好的文章解释路由- http://www.windowsnetworking.com/articles_tutorials/Making-Sense-Windows-Routing-Tables.html

    希望这有帮助。

        3
  •  3
  •   abatishchev Karl Johan    13 年前
    using System.Linq;
    using System.Net.NetworkInformation;
    
    var nic = NetworkInterface
         .GetAllNetworkInterfaces()
         .FirstOrDefault(i => i.NetworkInterfaceType != NetworkInterfaceType.Loopback && i.NetworkInterfaceType != NetworkInterfaceType.Tunnel);
    var name = nic.Name;
    

    或更优雅的解决方案:

    .Where(i => !(
        new[] { NetworkInterfaceType.Loopback, NetworkInterfaceType.Tunnel }
        .Contains(i.NetworkInterfaceType)))
    

    static IEnumerable<NetworkInterface> GetAllNetworkInterfaces(IEnumerable<NetworkInterfaceType> excludeTypes)
    {
        var all = NetworkInterface.GetAllNetworkInterfaces();
        var exclude = all.Where(i => excludeTypes.Contains(i.NetworkInterfaceType));
        return all.Except(exclude);
    }
    

    用法:

    var nic = GetAllNetworkInterfaces(new[] { NetworkInterfaceType.Tunnel, NetworkInterfaceType.Loopback });
    
        4
  •  2
  •   Hans Olsson    14 年前

    您可以使用WMI类 Win32_NetworkAdapter 枚举所有适配器,它有一个 Index 是默认值,或者其他属性之一可能有助于查找默认值。

    可能是这样的:

    编辑:修复了损坏的代码(这至少更有可能工作)。但是按照阿巴蒂什切夫说的,我认为你可能需要使用 Win32_NetworkAdapterConfiguration.IPConnectionMetric 要查找默认适配器。。。

    ManagementClass mc = new ManagementClass("Win32_NetworkAdapter");
    foreach (ManagementObject mo in mc.GetInstances())
    {
         int index = Convert.ToInt32(mo["Index"]);
         string name = mo["NetConnectionID"] as string;
         if (!string.IsNullOrEmpty(name))
              textBox1.Text += name + Environment.NewLine;
    }
    
        5
  •  1
  •   Community CDub    10 年前

    我的一个朋友也有同样的问题。玩了一下c#,我们似乎找到了一种跳过虚拟Ip(不是真正连接的Ip)的方法:我们寻找Gateway,不仅丢弃那些没有虚拟Ip的,还丢弃那些有虚拟Ip(0.0.0.0)的。在我的机器上,最后两个Vm-Ware虚拟适配器。

    public static void DisplayIPAddresses()
        {
    
            Console.WriteLine("\r\n****************************");
            Console.WriteLine("     IPAddresses");
            Console.WriteLine("****************************");
    
    
            StringBuilder sb = new StringBuilder();
            // Get a list of all network interfaces (usually one per network card, dialup, and VPN connection)     
            NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
    
            foreach (NetworkInterface network in networkInterfaces)
            {
    
                if (network.OperationalStatus == OperationalStatus.Up )
                {
                    if (network.NetworkInterfaceType == NetworkInterfaceType.Tunnel) continue;
                    //GatewayIPAddressInformationCollection GATE = network.GetIPProperties().GatewayAddresses;
                    // Read the IP configuration for each network   
    
                    IPInterfaceProperties properties = network.GetIPProperties();
                    //discard those who do not have a real gateaway 
                    if (properties.GatewayAddresses.Count > 0)
                    {
                        bool good = false;
                        foreach  (GatewayIPAddressInformation gInfo in properties.GatewayAddresses)
                        {
                            //not a true gateaway (VmWare Lan)
                            if (!gInfo.Address.ToString().Equals("0.0.0.0"))
                            {
                                sb.AppendLine(" GATEAWAY "+ gInfo.Address.ToString());
                                good = true;
                                break;
                            }
                        }
                        if (!good)
                        {
                            continue;
                        }
                    }
                    else {
                        continue;
                    }
                    // Each network interface may have multiple IP addresses       
                    foreach (IPAddressInformation address in properties.UnicastAddresses)
                    {
                        // We're only interested in IPv4 addresses for now       
                        if (address.Address.AddressFamily != AddressFamily.InterNetwork) continue;
    
                        // Ignore loopback addresses (e.g., 127.0.0.1)    
                        if (IPAddress.IsLoopback(address.Address)) continue;
    
                        if (!address.IsDnsEligible) continue;
                        if (address.IsTransient) continue; 
    
                        sb.AppendLine(address.Address.ToString() + " (" + network.Name + ") nType:" + network.NetworkInterfaceType.ToString()     );
                    }
                }
            }
            Console.WriteLine(sb.ToString());
        }
    
        6
  •  0
  •   leppie    14 年前

    您可以得到它们的列表,但不是默认值(也许您可以假设它是第一个条目)。

    static void Main(string[] args)
    {
      foreach (var nc in NetworkInterface.GetAllNetworkInterfaces())
      {
        Console.WriteLine(nc.Name);
      }
    
      Console.ReadLine();
    }