【发布时间】:2010-09-14 03:41:26
【问题描述】:
我需要使用 C# 和 .NET 3.5 从我的程序中获取计算机的实际本地网络 IP 地址(例如 192.168.0.220)。在这种情况下,我不能只使用 127.0.0.1。
我怎样才能做到这一点?
【问题讨论】:
标签: c# .net .net-3.5 ip-address
我需要使用 C# 和 .NET 3.5 从我的程序中获取计算机的实际本地网络 IP 地址(例如 192.168.0.220)。在这种情况下,我不能只使用 127.0.0.1。
我怎样才能做到这一点?
【问题讨论】:
标签: c# .net .net-3.5 ip-address
在 John Spano 的 How to get IP addresses in .NET with a host name 中,它说要添加 System.Net 命名空间,并使用以下代码:
//To get the local IP address string sHostName = Dns.GetHostName (); IPHostEntry ipE = Dns.GetHostByName (sHostName); IPAddress [] IpA = ipE.AddressList; for (int i = 0; i < IpA.Length; i++) { Console.WriteLine ("IP Address {0}: {1} ", i, IpA[i].ToString ()); }
【讨论】:
使用 Dns 需要在本地 DNS 服务器上注册您的计算机,如果您在 Intranet 上,则不一定如此,如果您在家中使用 ISP,则更不可能。它还需要网络往返——所有这些都是为了找出有关您自己计算机的信息。
正确的方法:
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
foreach(NetworkInterface adapter in nics)
{
foreach(var x in adapter.GetIPProperties().UnicastAddresses)
{
if (x.Address.AddressFamily == AddressFamily.InterNetwork && x.IsDnsEligible)
{
Console.WriteLine(" IPAddress ........ : {0:x}", x.Address.ToString());
}
}
}
(2015 年 7 月 31 日更新:修复了代码的一些问题)
或者对于那些只喜欢一行 Linq 的人:
NetworkInterface.GetAllNetworkInterfaces()
.SelectMany(adapter=> adapter.GetIPProperties().UnicastAddresses)
.Where(adr=>adr.Address.AddressFamily == AddressFamily.InterNetwork && adr.IsDnsEligible)
.Select (adr => adr.Address.ToString());
【讨论】:
NetworkInformation 从你的回答行号:2,c# 编译器说“找不到类型或命名空间”,我已经导入了 System.Net.NetworkInformation,然后它也说“类型或缺少命名空间”,这是怎么回事?
foreach ( NetworkInterface ni in nics ) { foreach ( UnicastIPAddressInformation x in ni.GetIPProperties().UnicastAddresses ) { if ( x.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork ) { Console.WriteLine( " IPAddress ........ : {0:x}", x.Address.ToString() ); } } },它打印出我的大部分实际 IP 地址和其他一些非我的 IP 地址的混合体。
如果您知道您的计算机有一个或多个 IPv4 地址,这将提供其中之一:
Dns.GetHostAddresses(Dns.GetHostName())
.First(a => a.AddressFamily == AddressFamily.InterNetwork).ToString()
GetHostAddresses 通常会在查询 DNS 服务器时阻塞调用线程,如果查询失败则抛出 SocketException。不知道是不是在查找自己的主机名时跳过了网络调用。
【讨论】:
如果您正在寻找命令行实用程序 ipconfig 可以提供的信息,您可能应该使用 System.Net.NetworkInformation 命名空间。
此示例代码将枚举所有网络接口并转储每个适配器的已知地址。
using System;
using System.Net;
using System.Net.NetworkInformation;
class Program
{
static void Main(string[] args)
{
foreach ( NetworkInterface netif in NetworkInterface.GetAllNetworkInterfaces() )
{
Console.WriteLine("Network Interface: {0}", netif.Name);
IPInterfaceProperties properties = netif.GetIPProperties();
foreach ( IPAddress dns in properties.DnsAddresses )
Console.WriteLine("\tDNS: {0}", dns);
foreach ( IPAddressInformation anycast in properties.AnycastAddresses )
Console.WriteLine("\tAnyCast: {0}", anycast.Address);
foreach ( IPAddressInformation multicast in properties.MulticastAddresses )
Console.WriteLine("\tMultiCast: {0}", multicast.Address);
foreach ( IPAddressInformation unicast in properties.UnicastAddresses )
Console.WriteLine("\tUniCast: {0}", unicast.Address);
}
}
}
您可能对 UnicastAddresses 最感兴趣。
【讨论】:
由于一台机器可以有多个 IP 地址,因此找出您将用于路由到一般互联网的 IP 地址的正确方法是打开一个连接到互联网上主机的套接字,然后检查socket 连接以查看该连接中使用的本地地址是什么。
通过检查套接字连接,您将能够考虑到奇怪的路由表、多个 IP 地址和古怪的主机名。上面的主机名技巧可以工作,但我认为它并不完全可靠。
【讨论】: