Returns a country code based on an IP address.

How to use

Step 1: Configure MaxMind license key

	<countryLookup maxMindLicenseKey="abcdefg01234" />

See configuration reference to use the <sharpNick> node. To get a MaxMind license key, sign up a site license with update subscriptions.

Step 2: Set read/write permissions on App_Data

Check this MSDN article to find out how to do that, if you've not done that already.

Step 3: Use!

var country = CountryLookup.GetCountryCode("");
Console.WriteLine(country); // US

Automatic updating

The MaxMind geoip database will go out of date within weeks. MaxMind provides a new database about every week. This class automatically downloads and replaces the old database file at 7am (computer time zone) every day.

This class downloads the database file on first use, which blocks off the calling thread (and any other new calling threads) until the file is downloaded and made available.

Location of database file

On a ASP.NET web application, the file will be downloaded to the app's App_Data directory. For non web applications, the file will be downloaded to the executing directory.


Use GeographyWizard to get the name of the country returned:

var country = CountryLookup.GetCountryCode("");
var name = GeographyWizard.GetCountryName(country);
Console.WriteLine(name); // United States


The code that queries the database file is based on Ting Huang's Java port, and is listed on MaxMind.

Last edited Oct 23, 2010 at 4:51 AM by weiwong, version 14


No comments yet.