15

I am using this standard code for populating list of countries:

static void Main(string[] args)
{
    List cultureList = new List();

    CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures);

    foreach (CultureInfo culture in cultures)
    {
        try
        {
            RegionInfo region = new RegionInfo(culture.LCID);

            if (!(cultureList.Contains(region.EnglishName)))
            {
                cultureList.Add(region.EnglishName);
                Console.WriteLine(region.EnglishName);
            }
        }
        catch (ArgumentException ex) 
        {
            // just ignore this
            continue;
        }
    }
}

I saw that some countries are missed. Just wondered what's the reason of such situation?

Yves M.
  • 29,855
  • 23
  • 108
  • 144
sashaeve
  • 9,387
  • 10
  • 48
  • 61
  • sashaeve, a concrete example would be nice. IIRC it depends on the Windows version/edition as well. – H H Jun 29 '10 at 09:43
  • @Henk Holterman: I did not find Ghana, Cote d'Ivoire, Cameroon and some others. – sashaeve Jun 29 '10 at 10:03
  • 1
    possible duplicate of [Missing Countries & locations from CultureInfo when trying to ](http://stackoverflow.com/questions/2920274/missing-countries-locations-from-cultureinfo-when-trying-to) – Lucero Jun 29 '10 at 10:20
  • until the release of vista, Greenland was not among them either! :) – Pauli Østerø Dec 22 '10 at 14:58

3 Answers3

8

The answer is: By design

CultureInfo.GetCultures is not designed to be a complete and definitive list of all the cultures in the world. It's only designed to get you the cultures that can be found on the computer.

CultureInfo documentation says:

Remember that the culture names and identifiers represent only a subset of cultures that can be found on a particular computer. Windows versions or service packs can change the available cultures. Applications add custom cultures using the CultureAndRegionInfoBuilder class. Users add their own custom cultures using the Microsoft Locale Builder tool. Microsoft Locale Builder is written in managed code using the CultureAndRegionInfoBuilder class.


Notes

Links on the MSDN that may be usefull:

And by the way, you can shorten your code with a simple LINQ 'command':

var regionInfos = CultureInfo.GetCultures(CultureTypes.SpecificCultures)
  .Select(c => new RegionInfo(c.LCID))
  .Distinct()
  .ToList();
Yves M.
  • 29,855
  • 23
  • 108
  • 144
2

You are not getting all cultures:

CultureTypes.AllCultures & ~CultureTypes.NeutralCultures
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • 1
    Oded, in the context of the question you're wrong. Neutral cultures don't represent a country, but only a language, and they are very limited in their use (mostly useful only for localizing resources or to get a specific culture from them). – Lucero Jun 29 '10 at 09:55
  • @Lucero - he is doing a bitwise complement on `NeutralCultures`. – Oded Jun 29 '10 at 09:56
  • 1
    which is correct because he does *not* want the neutral cultures in the result: `Console.WriteLine(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures);` returns `SpecificCultures, InstalledWin32Cultures` which should include all specific cultures (e.g. those with countries). – Lucero Jun 29 '10 at 10:17
1

I would use CultureTypes.SpecificCultures but it does not answer your question.

Why there is only subset of world's countries? Well, there are so many of them. Somebody would have to maintain them and it does cost money. I think that's why Microsoft decided to support only the most "popular" ones.

BTW. You may create your own CultureInfo. Also, I haven't tried, but you can create RegionInfo instance by passing its ISO code in constructor. I am not sure what will happen if there is no matching CultureInfo, though.

Paweł Dyda
  • 18,366
  • 7
  • 57
  • 79