按国家/地区名称获取RegionInfo?

我想通过执行以下操作来获取RegionInfo

 new RegionInfo("United Kingdom"); 

但这引发了一个例外,并表示它无法识别。

RegionInfo上的此页面表示如果“name不是有效的国家/地区名称”,则会引发exception。

然而,此页面指定了包含United Kingdom的类所使用的预定义区域列表,那么为什么不创建具有国家/地区名称的新RegionInfo呢?

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

如果您想通过国家/地区名称获取RegionInfo ,您可以获得IEnumerable ,然后根据上面的EnglishName进行过滤。 这使您能够填充combobox等内容。

来自MSDN ;

包含ISO 3166中针对国家/地区定义的双字母代码的字符串。

UNITED KINGDOM在ISO网站上的国家名称和代码元素上看起来不错。

GB英国

试试;

 new RegionInfo("GB"); 

您链接的同一页面也说:

RegionInfo名称是ISO 3166中针对国家/地区定义的双字母代码之一。 案件并不重要; 但是,Name,TwoLetterISORegionName和ThreeLetterISORegionName属性以大写forms返回相应的代码。

代码在页面上, GB似乎是英国的2字母代码(代码顺序很难搜索!)。 所以试试这个:

 new RegionInfo("GB"); 

或者,如果您使用的是.NET 2.0+,建议您使用完整的文化名称:

 new RegionInfo("en-GB"); 

如果我导航到构造函数,我在Visual Studio中看到的摘要说:

name :包含ISO 3166中针对country / region定义的双字母代码的字符串.-或 – 包含特定区域性,自定义区域性或仅Windows文化的区域性名称的字符串。 如果文化名称不是RFC 4646格式 ,则应用程序应指定整个区域性名称,而不仅仅是国家 /地区。

整个文化名称为’en-GB’。

或者你可以使用’GB’

查看MSDN页面:

A string containing one of the two-letter codes defined in ISO 3166 for country/region.

您需要英国的ISO 3166代码,而不是国家/地区的名称。

这是您需要的代码 。

请注意参数name的元数据中的此注释,该注释解释了.NET Framework 2.0的更改:

  // A string containing one of the two-letter codes defined in ISO 3166 for country/region.-or-Beginning // in .NET Framework version 2.0, a string containing the culture name for a // specific culture, custom culture, or Windows-only culture. If the culture // name is not in RFC 4646 format, your application should specify the entire // culture name, not just the country/region.