Friday, December 18, 2009

How to read all countries using C#

We can use CultureInfo & RegionInfo class for getting all the countries information. From the CultureInfo class, we can get all the Culture information available in .Net Framework. Then we can use RegionInfo class for reading all the information about the region along with the Country name. Please use the following code snippet to achieve this.

static void Main(string[] args)
{
    List<string> countriesList = new List<string>();
    foreach (CultureInfo culture in CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures))
    {
        if (!string.IsNullOrEmpty(culture.Name))
        {
            RegionInfo ri = new RegionInfo(culture.LCID);
            if (!countriesList.Contains(ri.EnglishName))
            countriesList.Add(ri.EnglishName);
        }
    }

    countriesList.Sort();           

    foreach (string str in countriesList)
        Console.WriteLine(str);          

    Console.ReadLine();
}


No comments: