coders
coders

Reputation: 731

How to get the country code using country name

I want to be able to get country code using country name. As of now I'm doing this:

var regions = CultureInfo.GetCultures(CultureTypes.SpecificCultures)
                         .Select(x => new RegionInfo(x.LCID));

var selectedRegion = regions.FirstOrDefault(region => 
                                                region.EnglishName.Contains(countryName));

Where CountryName in my case is "Tanzania". It is not giving the country code even though in the regions it is present. It always returns null.

Can you please suggest/enlighten me on how to get this done.

Upvotes: 0

Views: 546

Answers (1)

Dan D
Dan D

Reputation: 2523

The CultureInfo type has an EnglishName property which does contain an English (Tanzania) but as far as I can see not the region.

var cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
var tanzaniaCulture = cultures.FirstOrDefault(i => i.EnglishName == "English (Tanzania)");

Upvotes: 5

Related Questions