简体   繁体   English

如何从.NET中的CultureInfo获取ISO 3166国家/地区代码

[英]How to get ISO 3166 Country Code from CultureInfo in .NET

Given a CultureInfo object, how do I get the two character ISO 3166 Country Code? 给定一个CultureInfo对象,如何获得两个字符的ISO 3166国家/地区代码? Eg from en-US , I want US and from en-GB , I want GB . 例如,从en-US US ,从en-GB GB I also need to handle cases where the culture may not have a country code. 我还需要处理文化可能没有国家/地区代码的情况。

Use RegionInfo-class to retrieve two (or three) letter iso region name: 使用RegionInfo-class检索两个(或三个)字母iso区域名称:

RegionInfo usa = new RegionInfo("en-US");           
string isoUSA = usa.TwoLetterISORegionName;

RegionInfo gb = new RegionInfo("en-GB");
string isoGB = gb.TwoLetterISORegionName;

You might want to catch possible exception that will happen if you try to pass invalid value to the constructor. 如果您尝试将无效值传递给构造函数,则可能希望捕获可能发生的异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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