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 683 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
- 286,951
- 70
- 623
- 758
-
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,287
- 1
- 40
- 62