Get public/external IP address in C#

1) Create a simple form with one button and one textbox.
2) Code

private void button1_Click(object sender, EventArgs e)
{
	ThreadStart starter = delegate { Request(); };
	Thread thread = new Thread(starter);
	thread.Start();
}
public void Request()
{
	WebRequest request = WebRequest.Create("http://checkip.dyndns.org");
	WebResponse response = request.GetResponse();
	StreamReader stream = new StreamReader(response.GetResponseStream());
 
	Invoke((MethodInvoker)delegate
	{
		textBox1.Text = stream.ReadToEnd().
		Replace("<html><head><title>Current IP Check</title></head><body>Current IP Address: ", string.Empty).
		Replace("</body></html>", string.Empty);
	});
}
Categories: C# Tags: , , , ,
%d bloggers like this: