How to get ip address from .Net code
I just want to get the System IP address and display it on a UI.Can someone please tell the API to be used for the开发者_开发知识库 same
var address = Dns.GetHostAddresses(Dns.GetHostName())
.FirstOrDefault(addr => !IPAddress.IsLoopback(addr));
Console.WriteLine(address);
(this code excludes the local address 127.0.0.1)
internal IPAddress[] GetIPAddresses()
{
string hostName = System.Net.Dns.GetHostName();
IPHostEntry ihe = System.Net.Dns.GetHostEntry(hostName);
return ihe.AddressList;
}
精彩评论