Finding the user’s current region using RegionInfo in .NET C#

The CultureInfo object helps a lot in finding information about the user’s current culture. However, on occasion it may not be enough and you need to find out more about that user’s regional characteristics. You can easily retrieve a RegionInfo object from CultureInfo which will hold information about a particular country or region.

You can find the current region in two ways from CultureInfo:

CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
RegionInfo regionInfo = new RegionInfo(cultureInfo.LCID);
// or 
regionInfo = new RegionInfo(cultureInfo.Name);

string englishName = regionInfo.EnglishName;
string currencySymbol = regionInfo.CurrencySymbol;
string currencyEnglishName = regionInfo.CurrencyEnglishName;
string currencyLocalName = regionInfo.CurrencyNativeName;

My computer is set to use Swedish-Sweden as the specific culture so I get the following values from top to bottom:

  • Sweden
  • kr
  • Swedish krona
  • Svensk krona

If I change the current culture to my home country, i.e. Hungary…

CultureInfo hungaryCulture = new CultureInfo("hu-HU");
Thread.CurrentThread.CurrentCulture = hungaryCulture;
regionInfo = new RegionInfo(hungaryCulture.LCID);
englishName = regionInfo.EnglishName;
currencySymbol = regionInfo.CurrencySymbol;
currencyEnglishName = regionInfo.CurrencyEnglishName;
currencyLocalName = regionInfo.CurrencyNativeName;

…then the values are of course adjusted accordingly:

  • Hungary
  • Ft
  • Hungarian Forint
  • forint

Read all posts related to Globalisation in .NET here.

Advertisements

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

4 Responses to Finding the user’s current region using RegionInfo in .NET C#

  1. Mehran says:

    Reblogged this on Coding Tips and commented:
    That’s really Cool !!

  2. kiquenet says:

    Not same Control Panel: Regional and language options->Standards and formats than Control Panel: Regional and language options->Location

    using (var regKeyGeo = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@”Control Panel\International\Geo”))
    {
    Response.Write(“Geo Nation ” + regKeyGeo.GetValue(“Nation”).ToString());
    }

    var iso2 = “US”;
    var cultureInfo = CultureInfo.GetCultures(CultureTypes.AllCultures).Where(c => c.Name.EndsWith(iso2)).ToList();

    foreach (System.Globalization.CultureInfo culture in cultureList)
    {

    Response.Write(” Culture for ” + iso2 + “: ” + culture.DisplayName
    + ” *** ” + culture.NativeName + ” ” + culture.TwoLetterISOLanguageName
    + ” – ” + culture.ThreeLetterISOLanguageName
    + ” – ” + culture.DateTimeFormat.FullDateTimePattern);

    }

    For US you get chr-US en-US es-US and haw-US : Cheroqui, English, Spanish, Hawai

    http://stackoverflow.com/questions/8926400/get-cultureinfo-object-from-country-name-or-regioninfo-object

  3. masasih says:

    Great article.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

ultimatemindsettoday

A great WordPress.com site

Elliot Balynn's Blog

A directory of wonderful thoughts

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: