【发布时间】: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 地址呢?
【问题讨论】:
-
请仔细阅读。见GetIPProperties
-
@JohnSaunders 好的,我已经检查了您的链接阅读它.. 并尝试过.. 但我没有得到 IPV4 地址!像 192.168.1.25 !!
-
好吧,这比我想象的要微妙一些。见IPGlobalProperties.GetUnicastAddresses
标签: c# ip-address network-interface