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

我需要知道如何得到所有的网络接口与他们的 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地址呢?

102815 次浏览
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。地址是一个 IPAddress,你想要的。

经过一些改进,这段代码为一个组合添加了任何接口:

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());
}
}
}
}
}

拉姆达的一句话:

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

这些答案将我引向正确的方向——感谢前面的作者! 对于任何寻找即插即用方法的人来说,以下是对我起作用的方法。

public System.Net.IPAddress GetIpAddress()
{
return NetworkInterface
.GetAllNetworkInterfaces()
.Where(n => n.OperationalStatus == OperationalStatus.Up)
.Where(n => n.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || n.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
.Where(n => n.Name == "Wi-Fi")
.SelectMany(n => n.GetIPProperties()?.UnicastAddresses)
.Where(n => n.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
.Select(g => g?.Address)
.Where(a => a != null)
.FirstOrDefault();
}


public System.Net.IPAddress GetDefaultGateway()
{
return NetworkInterface
.GetAllNetworkInterfaces()
.Where(n => n.OperationalStatus == OperationalStatus.Up)
.Where(n => n.NetworkInterfaceType != NetworkInterfaceType.Loopback)
.SelectMany(n => n.GetIPProperties()?.GatewayAddresses)
.Select(g => g?.Address)
.Where(a => a != null)
.FirstOrDefault();
}