5

重要编辑:再次回到这个主题。正如您所说,不应该有默认网卡,我试图了解是否有办法检测所有实际连接的网卡。

拥有我的物理接口的 MAC 地址是否有一种编程方式来获取接口名称/接口状态/等...

比如我的XP机器:

设备 Realtek RTL8139 系列 PCI 快速以太网 NIC MAC XXXX-XXXX-XXXX

XXXX-XXXX-XXXX是我所知道的

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

所以我正在寻找 XXXX-XXXX-XXXX 和本地连接之间的链接。

希望现在一切都清楚了。

谢谢大家!PS抱歉耽搁了……对所有人+1投票,请耐心等待!


老问题


大家好,我想通过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

等等...

EDIT2:@ho1 运行您的代码更改 Name 不存在的 FriendlyName 您将得到类似于后面列表的内容,不幸的是它似乎不是预期的输出

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
4

6 回答 6

5

正如其他人所提到的,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 将选择它,Local Area Connection 2因为它的度量值较低。

编辑:这是一篇很棒的文章,解释了路由 - http://www.windowsnetworking.com/articles_tutorials/Making-Sense-Windows-Routing-Tables.html
EDIT2:拼写。

希望这可以帮助。

于 2010-08-11T16:36:57.740 回答
3
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)))

或者如果你想在 LINQ 中练习:

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 });
于 2010-08-11T10:06:18.187 回答
2

您可以使用 WMI 类Win32_NetworkAdapter枚举所有适配器,它具有一个Index属性,这可能意味着带有 0 或 1的适配器Index是默认适配器,或者其他属性之一可能有助于找到默认适配器。

可能是这样的:

编辑:修复了损坏的代码(这至少更有可能工作)。但是按照 abatishchev 所说的,我认为您可能需要使用它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;
}
于 2010-08-11T10:13:11.097 回答
2

这是一种肮脏的做法,因为它可以通过合并 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()

于 2010-09-08T17:01:57.890 回答
1

我的一个朋友(Ciro DA)也有同样的问题。用 c# 玩了一下,我们似乎找到了一种跳过虚拟(没有真正连接的 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());
    }
于 2012-02-01T17:26:23.000 回答
0

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

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

  Console.ReadLine();
}
于 2010-08-11T09:57:25.093 回答