What is the easiest way to get my external IP-address in C#?
There's no built-in way to do it within the framework because it's hard to determine what the external/public IP address is. This of course is assuming your IP is NAT'ed behind some gateway.
One method would be to scrape a site like http://www.whatismyip.org/ using the WebClient class.
System.Net.WebClient client = new System.Net.WebClient();
string ip = client.DownloadString( "http://www.whatismyip.org" );
Console.Out.WriteLine( ip );
public static string GetExternalIP()
{
using (var wc = new System.Net.WebClient())
return wc.DownloadString("http://whatismyip.org");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With