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
Asked
Active
Viewed 659 times
2 Answers
7
var address = Dns.GetHostAddresses(Dns.GetHostName())
.FirstOrDefault(addr => !IPAddress.IsLoopback(addr));
Console.WriteLine(address);
(this code excludes the local address 127.0.0.1)
Thomas Levesque
- 278,830
- 63
- 599
- 738
-
You may want to change .First to .FirstOrDefault, in case the computer has no IP. – KristoferA Apr 06 '10 at 10:24
3
internal IPAddress[] GetIPAddresses()
{
string hostName = System.Net.Dns.GetHostName();
IPHostEntry ihe = System.Net.Dns.GetHostEntry(hostName);
return ihe.AddressList;
}
KristoferA
- 12,067
- 1
- 39
- 62