Shuric
Shuric

Reputation: 337

How to get current regional settings in C#?

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?

Upvotes: 27

Views: 62978

Answers (8)

mggSoft
mggSoft

Reputation: 1042

This simple code worked for me (avoiding caching):

// Clear cached data for the current culture
Thread.CurrentThread.CurrentCulture.ClearCachedData();

// In a new thread instance we get current culture.
// This code avoid getting wrong cached cultureinfo objects when user replaces some values in the regional settings without restarting the application
CultureInfo currentCulture = new Thread(() => { }).CurrentCulture;

Upvotes: 0

tjsmith
tjsmith

Reputation: 749

[DllImport("kernel32.dll")]
private static extern int GetUserDefaultLCID();

public static CultureInfo CurrentCultureInRegionalSettings => new CultureInfo(GetUserDefaultLCID());

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038800

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()

Upvotes: 31

Nicolas
Nicolas

Reputation: 17

There are the classes CultureInfo and TextInfo from the namespace System.Globalization. Both classes get several windows regional settings defined in the control panels. The list of available settings is in the documentation.

For example:

string separator = CultureInfo.CurrentCulture.TextInfo.ListSeparator;

is getting the list separator for the program which is running.

Upvotes: 1

Mark Green
Mark Green

Reputation: 61

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

Upvotes: 3

jjacka
jjacka

Reputation: 531

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.

Upvotes: 2

Arsen Mkrtchyan
Arsen Mkrtchyan

Reputation: 50712

Try to find settings you want in SystemInformation class or look into WMI using the classes in System.Management/System.Diagnostics, you can use LINQ to WMI too

Upvotes: 0

Christian Hayter
Christian Hayter

Reputation: 31071

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

Upvotes: 6

Related Questions