简体   繁体   中英

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.

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)");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM