How to get the country code from CultureInfo?

后端 未结 5 2059
面向向阳花
面向向阳花 2021-01-01 10:17

I have the following:

System.Globalization.CultureInfo c = new System.Globalization.CultureInfo(\"en-GB\");

var a = c.DisplayName;
var b = c.EnglishName;
va         


        
相关标签:
5条回答
  • 2021-01-01 10:41

    You can try the RegionInfo Class. One of the properties is the RegionInfo.TwoLetterISORegionName Property. Example from MSDN:

    RegionInfo myRI1 = new RegionInfo("US");
    Console.WriteLine( "   Name:                         {0}", myRI1.Name );
    Console.WriteLine( "   ThreeLetterISORegionName:     {0}", myRI1.ThreeLetterISORegionName );
    Console.WriteLine( "   TwoLetterISORegionName:       {0}", myRI1.TwoLetterISORegionName );
    

    Name: US

    ThreeLetterISORegionName: USA

    TwoLetterISORegionName: US

    0 讨论(0)
  • 2021-01-01 10:44
    var c = new CultureInfo("en-GB");
    var r = new RegionInfo(c.LCID);
    string name = r.Name;
    

    Most probably you need to use r.TwoLetterISORegionName property.

    string regionName = r.TwoLetterISORegionName;
    
    0 讨论(0)
  • 2021-01-01 10:44
    System.Globalization.CultureInfo c = new System.Globalization.CultureInfo("en-GB");
    var ri = new RegionInfo(c.Name);
    string countryName = ri.DisplayName;
    

    That will give you:

    "United Kingdom"
    

    For Two Letter Use:

    string countryAbbrivation = ri.TwoLetterISORegionName;
    

    That will give you "GB"

    0 讨论(0)
  • 2021-01-01 10:49

    If you just want to use the RegionInfo of the current thread, you can get the country code with this one-liner:

    RegionInfo.CurrentRegion.TwoLetterISORegionName
    
    0 讨论(0)
  • 2021-01-01 10:50

    Following will also accept CultureInfo("en");

    var c = new CultureInfo("en-GB");
    string countryAbbrivation;
    if (!c.IsNeutralCulture) 
    {
                        var region = new RegionInfo(ContentLanguage.PreferredCulture.LCID);
                        countryAbbrivation = region.TwoLetterISORegionName.ToLower();
    }else{
                        countryAbbrivation = c.Name;
    }
    
    0 讨论(0)
提交回复
热议问题