Determining the region by IP

How can I determine the region by IP in the console program?

Author: Nicolas Chabanovsky, 2015-10-11

1 answers

Take any free geolocation service, make a request to it and output the result:

using System;
using System.Net;
using System.Xml.Linq;

namespace ConsoleApplication27
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Waiting for location response...");

            var locationResponse = new WebClient().DownloadString("https://freegeoip.net/xml/");
            Console.WriteLine("Raw location response:");
            Console.WriteLine(locationResponse);

            var responseXml = XDocument.Parse(locationResponse)
                .Element("Response");

            Console.WriteLine("You are in {0}, {1}.", 
                responseXml.Element("City").Value,
                responseXml.Element("CountryName").Value);
        }
    }
}
 4
Author: PashaPash, 2015-10-11 13:07:15