C# 如何获取网络接口及其正确的 IPv4 地址?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9855230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the network interface and its right IPv4 address?
提问by Murhaf Sousli
I need to know how to get all network interfaces with their IPv4address. Or just wireless and Ethernet.
我需要知道如何使用它们的IPv4地址获取所有网络接口。或者只是无线和以太网。
To get all network interfaces details I use this:
要获取所有网络接口详细信息,我使用以下命令:
foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces()) {
if(ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 ||
ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet) {
Console.WriteLine(ni.Name);
}
}
And to get the all hosted IPv4 addresses of the computer:
并获取计算机的所有托管 IPv4 地址:
IPAddress [] IPS = Dns.GetHostAddresses(Dns.GetHostName());
foreach (IPAddress ip in IPS) {
if (ip.AddressFamily == AddressFamily.InterNetwork) {
Console.WriteLine("IP address: " + ip);
}
}
But how to get the network interface and its right ipv4 address?
但是如何获取网络接口及其正确的 ipv4 地址呢?
采纳答案by bwall
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());
}
}
}
}
This should get you what you want. ip.Address is an IPAddress, that you want.
这应该让你得到你想要的。ip.Address 是您想要的 IP 地址。
回答by Hady Mahmoodi
With some improvement, this code adds any interface to a combination:
经过一些改进,此代码向组合添加了任何接口:
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);
}
}
}
And when selecting one of them, this code returns the IP address of the interface:
选择其中一个时,此代码返回界面的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 Mc_Topaz
One line with Lamda:
与 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);

