1

Possible Duplicates:
how to get my own IP address in C#?
how to get ip address of machine in c#

Hi all, I am currently developing a c# application for windows using WPF. I would like to get the computers external IP address i.e. the internet address not the local computer ip address or the local router address.

Thanks for your help.

Community
  • 1
  • 1
Boardy
  • 35,417
  • 104
  • 256
  • 447

5 Answers5

5

Like stated earlier, you need an external web server. An easy call to HTTP GET with the URL "http://checkip.dyndns.org/" will get you a simple text string with your IP.

Eldad Mor
  • 5,405
  • 3
  • 34
  • 46
1

[EDIT] A simple request to here will get you your ip.

This is a way to get any network address (not necessarily the internet ip) as pointed out in the comments:

IPAddress host = IPAddress.None;
foreach (IPAddress ip in Dns.GetHostAddresses(Dns.GetHostName()))
    {
        host = ip;
        if (ip.AddressFamily == AddressFamily.InterNetwork)
             break;
    }
NickAldwin
  • 11,584
  • 12
  • 52
  • 67
  • This will retrieve the first IPv4 address of the local machine, not necessarily the external address of the machine (not even guaranteed to be a internet accessible interface). – sisve Dec 30 '10 at 21:34
  • Your edit answers the question. Remove the old code and you'll get an up vote from me. :) – Tim Lloyd Dec 30 '10 at 21:58
  • @chibacity I left it in so that Simon's comment wouldn't seem strange...and to clarify. – NickAldwin Dec 30 '10 at 22:17
1

You need to have a web server sitting somewhere in the cloud so that you can call and that will be able to give you your external IP address.

Looks like this one is free.

Community
  • 1
  • 1
Aliostad
  • 80,612
  • 21
  • 160
  • 208
1

The only way I have found is to do a httpWebRequest to http://www.whatismyip.com/automation/n09230945.asp and parse the results for the ip

dko
  • 874
  • 2
  • 7
  • 18
-1

You can try connecting to whatismyip.com, as shown in the code below:

using System;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace DreamInCode.Snippets
{
    public class IpFinder
    {
        public static IPAddress GetExternalIp()
        {
            string whatIsMyIp = "http://whatismyip.com";
            string getIpRegex = @"(?<=<TITLE>.*)\d*\.\d*\.\d*\.\d*(?=</TITLE>)";
            WebClient wc = new WebClient();
            UTF8Encoding utf8 = new UTF8Encoding();
            string requestHtml = "";
            try
            {
                requestHtml = utf8.GetString(wc.DownloadData(whatIsMyIp));
            }
            catch (WebException we)
            {
                // do something with exception
                Console.Write(we.ToString());
            }
            Regex r = new Regex(getIpRegex);
            Match m = r.Match(requestHtml);
            IPAddress externalIp = null;
            if (m.Success)
            {
                externalIp = IPAddress.Parse(m.Value);
            }
            return externalIp;
        }
    }
}

NOTE: The code comes from this post http://www.dreamincode.net/forums/topic/24692-showing-the-external-ip-address-in-c%23/ .

PeterParameter
  • 524
  • 5
  • 16
  • 1
    They specifically want you to not do that. See the comment in the web source: `` Which does not require scraping -- because it is simply plain text of the ip! Do they even still include the IP in the title? – NickAldwin Dec 30 '10 at 21:41