【问题标题】:How do I get the network interface and its right IPv4 address?如何获取网络接口及其正确的 IPv4 地址?
【发布时间】:2012-04-08 23:01:21
【问题描述】:

我需要知道如何使用IPv4 地址获取所有网络接口。 或者只是无线和以太网。

要获取所有网络接口的详细信息,我使用这个:

foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces()) {
    if(ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 ||
       ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet) {

        Console.WriteLine(ni.Name);
    }
}

并获取计算机的所有托管 IPv4 地址:

IPAddress [] IPS = Dns.GetHostAddresses(Dns.GetHostName());
foreach (IPAddress ip in IPS) {
    if (ip.AddressFamily == AddressFamily.InterNetwork) {

        Console.WriteLine("IP address: " + ip);
    }
}

但是如何获取网络接口及其正确的 ipv4 地址呢?

【问题讨论】:

标签: c# ip-address network-interface


【解决方案1】:
foreach(NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
{
   if(ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
   {
       Console.WriteLine(ni.Name);
       foreach (UnicastIPAddressInformation ip in ni.GetIPProperties().UnicastAddresses)
       {
           if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
           {
               Console.WriteLine(ip.Address.ToString());
           }
       }
   }  
}

这应该可以得到你想要的。 ip.Address 是您想要的 IPAddress。

【讨论】:

【解决方案2】:

经过一些改进,此代码将任何接口添加到组合中:

private void LanSetting_Load(object sender, EventArgs e)
{
    foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
    {
        if ((nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet) || (nic.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)) //&& (nic.OperationalStatus == OperationalStatus.Up))
        {
            comboBoxLanInternet.Items.Add(nic.Description);
        }
    }
}

以及选择其中一个时,此代码返回接口的IP地址:

private void comboBoxLanInternet_SelectedIndexChanged(object sender, EventArgs e)
{
    foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
    {
        foreach (UnicastIPAddressInformation ip in nic.GetIPProperties().UnicastAddresses)
        {
            if (nic.Description == comboBoxLanInternet.SelectedItem.ToString())
            {
                if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                {
                    MessageBox.Show(ip.Address.ToString());
                }
            }
        }
    }
}

【讨论】:

    【解决方案3】:

    与Lamda一行:

    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Sockets;
    using System.Net.NetworkInformation;
    
    var ipV4s = NetworkInterface.GetAllNetworkInterfaces()
        .Select(i => i.GetIPProperties().UnicastAddresses)
        .SelectMany(u => u)
        .Where(u => u.Address.AddressFamily == AddressFamily.InterNetwork)
        .Select(i => i.Address);
    

    【讨论】:

      猜你喜欢
      • 2013-06-21
      • 1970-01-01
      • 2018-01-26
      • 2011-02-05
      • 2018-06-28
      • 2011-11-09
      • 2011-07-13
      • 2018-10-27
      • 1970-01-01
      相关资源
      最近更新 更多