开发者

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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜