Just in Chronicles

Life as a Voyage

How to Get List of Countries Defined in ISO 3166-1 Programatically by C#

with one comment

ISO 3166-1 defines the list of countries currently existing in the world while ISO 639-1 defines the list of languages currently and officially used all over the world. What if we need to display the list of countries do developers usually try? We can think of creating a meta table in a database or an XML file for reference.

Fortunately, .NET framework provides System.Globalization namespace to handle culture specific data such as date and time format, currency format and so forth. This is particularly useful for applications considering I18N or L10N.

There are two classes called CultureInfo and RegionInfo in the System.Globalization namespace. CultureInfo detects current system environment and returns the current culture including language, country, currency, time and date. It also returns a specific culture information by manually initialising the culture code. A CultureInfo object contains both language code defined by ISO 639-1 and country code defined by ISO 3166-1. By using this information, we can programmatically returns the list of countries.

/// <summary>
/// Gets the list of countries based on ISO 3166-1
/// </summary>
/// <returns>Returns the list of countries based on ISO 3166-1</returns>
public static List<RegionInfo> GetCountriesByIso3166()
{
	List<RegionInfo> countries = new List<RegionInfo>();
	foreach (CultureInfo culture in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
	{
		RegionInfo country = new RegionInfo(culture.LCID);
		if (countries.Where(p => p.Name == country.Name).Count() == 0)
			countries.Add(country);
	}
	return countries.OrderBy(p => p.EnglishName).ToList();
}

Let’s see the highlighted line 8 above. Firstly, we need list up all cultures and create RegionInfo objects based on the culture. As a CultureInfo object has both language code and country code and some CultureInfo objects have common country code, eg. en-CA (English in Canada), fr-CA (French in Canada), we need to take only one RegionInfo object (line 10-12).

/// <summary>
/// Gets the list of countries by selected country codes.
/// </summary>
/// <param name="code">List of culture codes.</param>
/// <returns>Returns the list of countries by selected country codes.</returns>
public static List<RegionInfo> GetCountriesByCode(List<string> codes)
{
	List<RegionInfo> countries = new List<RegionInfo>();
	if (codes != null && codes.Count > 0)
	{
		foreach (string code in codes)
		{
			try
			{
				countries.Add(new RegionInfo(code));
			}
			catch
			{
				//	Ignores the invalid culture code.
			}
		}
	}
	return countries.OrderBy(p => p.EnglishName).ToList();
}

We can also get the list of countries from the country codes provided like above. Make sure that we need to use the try...catch... block (line 19) to ignore the invalid country code used. This code can be more improved by using various types of codes.

Advertisements

Written by Justin Yoo

15/03/2012 at 08:00

One Response

Subscribe to comments with RSS.

  1. I just found this post and it appears to be exactly what I am looking for, though I cannot seem to get the list by GetCountriesByIso3166 to work as the codes for GetCountriesByCode. Any help would be appreciated.

    TheVeganG

    23/06/2016 at 18:21


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s