如何获得网络接口及其正确的IPv4地址?


71

我需要知道如何使用其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地址?


1
请仔细阅读。请参阅GetIPProperties
John Saunders

@JohnSaunders好吧,我已经检查了您的链接,然后阅读并尝试了..但是我没有得到IPV4地址!像192.168.1.25一样!!
Murhaf Sousli 2012年

3
好吧,这比我想的要微妙得多。参见IPGlobalProperties.GetUnicastAddresses
John Saunders

Answers:


117
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。


@Joseph,您的链接已死:/
流行于

3
@Felk谢谢,这是原始的网址gist.github.com/anonymous/ff82643c9a004281544a
Joseph

2

与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);

1

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

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());
                }
            }
        }
    }
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.