Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

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();
}


Wednesday, December 16, 2009

How to get all Culture information using C#

The CultureInfo class will provide us all Culture information available in .Net Framework.  You can use CultureInfo.Get­Cultures static method for getting all the culture information. To get associated specific culture, please use static method CultureInfo.Cre­ateSpecificCul­ture. 

The following example will show you how to get all culture information.

static void Main(string[] args)
{
     // Get culture names
     List<string> list = new List<string>();
     foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.AllCultures))
     {
          string specName = "(none)";
          try { specName = CultureInfo.CreateSpecificCulture(ci.Name).Name;}
          catch(Exception) { }

          list.Add(string.Format("{0,-12}{1,-12}{2}", ci.Name, specName, ci.EnglishName));
     }

     list.Sort();

     Console.WriteLine("CULTURE  SPEC.CULTURE  ENGLISH NAME");
     Console.WriteLine("----------------------------------------------------");

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

     Console.ReadLine();
}