如何以编程方式获取计算机的本地IP地址?

问题描述:

我需要使用C#和.NET 3.5从我的程序中获取计算机的实际本地网络IP地址(例如192.168.0.220)。在这种情况下,我不能只使用127.0.0.1。

I need to get the actual local network IP address of the computer (e.g. 192.168.0.220) from my program using C# and .NET 3.5. I can't just use 127.0.0.1 in this case.

执行此操作的最佳方法是什么?

What's the best way to do this?

链接
它说在那里,添加System.net,并使用以下

Link It says there, add System.net, and using the following

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