c# get my public ip code example
Example 1: c# get pc ip address
public static string GetLocalIPAddress()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
throw new Exception("No network adapters with an IPv4 address in the system!");
}
Example 2: get sites ip in C#
var url = "https://www.google.com/";
Uri myUri = new Uri(url);
var ip = Dns.GetHostAddresses(myUri.Host)[0];