简体   繁体   中英

Get country name from country code in .net

I know how to get the country name with a country code:

new RegionInfo("de").EnglishName

But how can i get the country code with a given english-name?

This is as simple as that:

var name = "Germany";
var allRegions = CultureInfo.GetCultures(CultureTypes.SpecificCultures).Select(x => new RegionInfo(x.LCID));
var germanyCode = allRegions .FirstOrDefault(region => region.EnglishName == name)?.Name;

Note, that when the germanyCode is null then somebody messed up the English name.

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