我如何获得网络接口和正确的IPv4地址? [英] How do I get the network interface and its right IPv4 address?
本文介绍了我如何获得网络接口和正确的IPv4地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要知道如何让他们的IPv4地址的所有网络接口。 或只是无线和以太网。
要得到我用这所有的网络接口的细节:
的foreach(NetworkInterface的妮NetworkInterface.GetAllNetworkInterfaces()){
如果(ni.NetworkInterfaceType == || NetworkInterfaceType.Wireless80211
ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet){ Console.WriteLine(ni.Name);
}
}
和拿到电脑的所有托管的IPv4地址:
ip地址[] = IPS Dns.GetHostAddresses(Dns.GetHostName());
的foreach(ip地址IP在IPS){
如果(ip.AddressFamily == AddressFamily.InterNetwork){ Console.WriteLine(IP地址+ IP);
}
}
可是如何才能让网络接口和正确的IPv4地址?
解决方案
的foreach(NetworkInterface的妮NetworkInterface.GetAllNetworkInterfaces())
{
如果(ni.NetworkInterfaceType == || NetworkInterfaceType.Wireless80211 == ni.NetworkInterfaceType NetworkInterfaceType.Ethernet)
{
Console.WriteLine(ni.Name);
的foreach(UnicastIPAddressInformation IP在ni.GetIPProperties()。UnicastAddresses)
{
如果(ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
Console.WriteLine(ip.Address.ToString());
}
}
}
}
这应该得到你想要的东西。 ip.Address是一个ip地址,你想要的。
I need to know how to get all network interfaces with their IPv4 address. Or just wireless and Ethernet.
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:
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?
解决方案
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.
这篇关于我如何获得网络接口和正确的IPv4地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文