Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get RegionInfo by country name?

I want to be able to get RegionInfo by doing the following:

new RegionInfo("United Kingdom");

but this throws an exception and says that it is not recognised.

This page on RegionInfo says that an exception is thrown if 'name is not a valid country/region name'.

And yet this page specifies a list of predefined regions used by the class that and contains United Kingdom, so why doesn't creating a new RegionInfo with country name work?

like image 606
DevDave Avatar asked Jan 10 '13 16:01

DevDave


3 Answers

  var regions = CultureInfo.GetCultures(CultureTypes.SpecificCultures).Select(x => new RegionInfo(x.LCID));
  var englishRegion = regions.FirstOrDefault(region => region.EnglishName.Contains(name));

If you want to get RegionInfo by the country name, you could get an IEnumerable<RegionInfo> and then filter based on the EnglishName as above. This gives you the ability to populate things such as comboboxes too.

like image 128
LukeHennerley Avatar answered Sep 22 '22 04:09

LukeHennerley


That same page you linked also says:

The RegionInfo name is one of the two-letter codes defined in ISO 3166 for country/region. Case is not significant; however, the Name, the TwoLetterISORegionName, and the ThreeLetterISORegionName properties return the appropriate code in uppercase.

The codes are on the page, and GB appears to be the 2 letter code for the UK (it's in code order to be difficult searching!). So try this:

new RegionInfo("GB");

Or if you're using .NET 2.0+, it's recommended you use the full culture name:

new RegionInfo("en-GB");
like image 3
Bridge Avatar answered Sep 21 '22 04:09

Bridge


From MSDN;

A string that contains a two-letter code defined in ISO 3166 for country/region.

UNITED KINGDOM looks ok on Country names and code elements on the ISO website.

GB UNITED KINGDOM

Try with;

new RegionInfo("GB");
like image 2
Soner Gönül Avatar answered Sep 19 '22 04:09

Soner Gönül