Possible Duplicate:
c# Get public/external IP address?
I am developing an application in c# in which I need to find the public IP address used to connect to the internet. How can i get this?
Possible Duplicate:
c# Get public/external IP address?
I am developing an application in c# in which I need to find the public IP address used to connect to the internet. How can i get this?
You can use http://showip.codebrainz.ca/ to get it. More info here.
Code example:
using System;
using System.Net;
class Program
{
static void Main()
{
// Create web client.
WebClient client = new WebClient();
// Download string.
string ip = client.DownloadString("http://showip.codebrainz.ca/");
// Write ip.
Console.WriteLine(ip);
}
}
The simplest ways of getting the public IP address have already been addressed by the other posters. There are some alternatives but they are situational (such as SNMP which requires an SNMP capable router).
If you want a method which not only allows you to get the public IP address but also allows you to test how you are connected to the internet (e.g Symmetric NAT, Full Cone, direct connection, etc.) you could use the STUN protocol (see RFC 5389). STUN also requires you to contact an external STUN server.
Ivar Lumi posted some working c# code over at the codeproject if you want an example. Depending on your requirements this may be overkill.