Search code examples
.netcountry-codes

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?


Solution

  • 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.