Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current regional settings in C#?

Tags:

c#

cultureinfo

Normally you can get it by writing something like

CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;

But this way you can only get CultureInfo which was configured at the moment application was launched and will not update if the setting have been changed afterwards.

So, how to get CultureInfo currently configured in Control Panel -> Regional and Language Settings?

like image 919
Shuric Avatar asked Oct 09 '09 07:10

Shuric


People also ask

How do I get current region in C#?

WriteLine("ISOCurrencySymbol:" + regionInfo. ISOCurrencySymbol); Console. WriteLine("Name:" + regionInfo.Name); This reads the information you have set in Windows 10 under "Regional & Language" > "Country or region".

How do I know what region my computer is?

Click the Start button, and then click Control Panel. Click Clock, Language, and Region, and then click Regional and Language Options.

What are region settings?

regional settings. the settings in an operating system that pertain to the user's location, such as language, currency, and time zone.


4 Answers

As @Christian proposed ClearCachedData is the method to use. But according to MSDN:

The ClearCachedData method does not refresh the information in the Thread.CurrentCulture property for existing threads

So you will need to first call the function and then start a new thread. In this new thread you can use the CurrentCulture to obtain the fresh values of the culture.

class Program
{
    private class State
    {
        public CultureInfo Result { get; set; }
    }

    static void Main(string[] args)
    {
        Thread.CurrentThread.CurrentCulture.ClearCachedData();
        var thread = new Thread(
            s => ((State)s).Result = Thread.CurrentThread.CurrentCulture);
        var state = new State();
        thread.Start(state);
        thread.Join();
        var culture = state.Result;
        // Do something with the culture
    }

}

Note, that if you also need to reset CurrentUICulture, you should do it separately

Thread.CurrentThread.CurrentUICulture.ClearCachedData()
like image 186
Darin Dimitrov Avatar answered Oct 22 '22 23:10

Darin Dimitrov


Thread.CurrentThread.CurrentCulture.ClearCachedData() looks like it will cause the culture data to be re-read when it is next accessed.

like image 27
Christian Hayter Avatar answered Oct 22 '22 22:10

Christian Hayter


You can use Win32 API function GetSystemDefaultLCID. The signiture is as follow:

[DllImport("kernel32.dll")]
static extern uint GetSystemDefaultLCID();

GetSystemDefaultLCID function returns the LCID. It can map language string from the folowing table. Locale IDs Assigned by Microsoft

like image 3
Mark Green Avatar answered Oct 23 '22 00:10

Mark Green


We ran into to this issue with our WinForms app and it was due to Visual Studio creating the [MyApp].vshost.exe process that is always running in the background whenever Visual Studio is open.

Turning off the MyApp -> Properties -> Debug -> "Enable Visual Studio hosting process" setting fixed this for us.

The vshost process is mainly used to improve debugging, but if you don't want disable the setting, you can kill the process as needed.

like image 2
jjacka Avatar answered Oct 22 '22 22:10

jjacka