37

I have the following:

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

var a = c.DisplayName;
var b = c.EnglishName;
var d = c.LCID;
var e = c.Name;
var f = c.NativeName;
var g = c.TextInfo;
var h = c.ThreeLetterISOLanguageName;
var i = c.ThreeLetterWindowsLanguageName;
var j = c.TwoLetterISOLanguageName;

None of this gives me the country code, e.g. GB.

Is there a way to get it without string splitting?

user247702
  • 23,641
  • 15
  • 110
  • 157
Miguel Moura
  • 36,732
  • 85
  • 259
  • 481

5 Answers5

93
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;
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • 5
    Note: You should check CultureInfo.IsNeutralCulture before feeding its LCID to RegionInfo. RegionInfos cannot be created from Neutral cultures (eg "en"). – Tor Haugen May 04 '21 at 10:37
  • 1
    Instead of new RegionInfo(c.LCID) it is better to use new RegionInfo(c.ToString()); because multiple cultures have the same LCID, e.g. both "en-GG" (Guernsey) and "en-JE" (Jersey) have an LCID of 4096 – Vdex Jun 17 '22 at 12:39
12
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"

Habib
  • 219,104
  • 29
  • 407
  • 436
  • 1
    He wanted "GB" - you gave him "United Kingdom". This might help him but isn't what was asked for... – Chris Dec 02 '13 at 14:18
  • 2
    @Chris, `string countryAbbrivation = ri.TwoLetterISORegionName;` would do it. added in the answer as well – Habib Dec 02 '13 at 14:19
6

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

Abbas
  • 14,186
  • 6
  • 41
  • 72
5

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
William
  • 1,993
  • 2
  • 23
  • 40
0

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;
}
RaSor
  • 869
  • 10
  • 11