Anyone know how to do this? Can it be done?
Why use Web browser control (Dont know if it can be done) when you can get IP address using
IPHostEntry ip = default(IPHostEntry);
ip = Dns.GetHostByName(Dns.GetHostName);
As Shoban suggests, if you're looking for the IP of the local machine, there's no need to use the browser control. I'd prefer the below to get the IP, though; GetHostByName
is marked as obsolete.
IPHostEntry entry = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in entry.AddressList)
{
Console.WriteLine(ip);
}
You can and often will see multiple IPs for the same host.
If you're looking for the IP of the machine you've navigated to in a WebBrowser control, grab the URL from the Url property of the control, and do the same thing:
Uri url = new Uri(browser.Url);
IPHostEntry entry = Dns.GetHostEntry(url.Host);
foreach (IPAddress ip in entry.AddressList)
{
Console.WriteLine(ip);
}
EDIT: Proximo indicates that he's looking for his computer's external IP address. The site WhatIsMyIp (thanks to Brian for the reference) presents the IP as the only returned content; nothing simpler to scrape:
WebClient wc = new WebClient();
string ip = wc.DownloadString("http://www.whatismyip.org/");
Console.WriteLine(ip);
Be kind to the site, and respect any usage restrictions they impose.
Hey, This is me actually. I try your method and it works but I am trying to get the IP address of a proxy that I am using. This method doesn't show the proxy IP address.