1

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

1

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

1 Comment

You may want to change .First to .FirstOrDefault, in case the computer has no IP.
3
internal IPAddress[] GetIPAddresses()
{
    string hostName = System.Net.Dns.GetHostName();
    IPHostEntry ihe = System.Net.Dns.GetHostEntry(hostName);
    return ihe.AddressList;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.