How to find my external IP address

If you computers are sitting behind a router/firewall, then running Ipconfig would not give you the external IP address. One can use http://www.whatsmyip.org/ for ad-hoc queries, but if you want to automate the checks you might want something simpler and easier to parse. The followings are the options I found.

The c# code to find out your external IP:

public string GetPublicIP() {
    String direction = "";
    WebRequest request = WebRequest.Create("http://checkip.dyndns.org/");
    using (WebResponse response = request.GetResponse())
    using (StreamReader stream = new StreamReader(response.GetResponseStream())) {
        direction = stream.ReadToEnd();
    }
 
    //Search for the ip in the html
    int first = direction.IndexOf("Address: ") + 9;
    int last = direction.LastIndexOf("</body>");
    direction = direction.Substring(first, last - first);
 
    return direction;
}

Leave a Reply

Your email address will not be published. Required fields are marked *