26

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?

Shuric
  • 327
  • 1
  • 4
  • 8

8 Answers8

31

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()
Michael Freidgeim
  • 26,542
  • 16
  • 152
  • 170
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I'm getting error The type or namespace name 'State' could not be found (are you missing a using directive or an assembly reference?) on the line: var thread = new Thread( s => ((State)s).Result = Thread.CurrentThread.CurrentCulture); The problem is on the State reference. Any idea on how to solve this? Tks – Pascal May 05 '10 at 11:10
  • @Pascal, `State` is a private class I've defined inside the `Program` class but you could try externalizing it into its own file and making it public. Also `State` is probably not a very good name, so you may try renaming it to something more meaningful. – Darin Dimitrov May 05 '10 at 16:31
  • nice but i do not understand this line var thread = new Thread( s => ((State)s).Result = Thread.CurrentThread.CurrentCulture); can u plzz explain. – Thomas May 15 '13 at 12:35
6

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

Christian Hayter
  • 30,581
  • 6
  • 72
  • 99
3

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

Mark Green
  • 61
  • 1
  • Similar to this, but I used GetUserDefaultLCID() instead which got me the user setting, I think the system default is the installed locale. – Colin Mar 18 '15 at 14:40
  • [DllImport("kernel32.dll")] static extern int GetSystemDefaultLCID(); var name = new System.Globalization.CultureInfo(GetSystemDefaultLCID()).Name; – Giles Bathgate Aug 31 '16 at 10:55
2

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.

jjacka
  • 523
  • 4
  • 9
1

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.

sth
  • 222,467
  • 53
  • 283
  • 367
Nicolas
  • 17
  • 2
1
[DllImport("kernel32.dll")]
private static extern int GetUserDefaultLCID();

public static CultureInfo CurrentCultureInRegionalSettings => new CultureInfo(GetUserDefaultLCID());
tjsmith
  • 729
  • 7
  • 21
0

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

Arsen Mkrtchyan
  • 49,896
  • 32
  • 148
  • 184
0

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;
mggSoft
  • 992
  • 2
  • 20
  • 35