How to get current regional settings in C#?
Asked Answered
M

8

27

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?

Moynahan answered 9/10, 2009 at 7:48 Comment(0)
L
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()
Linkage answered 9/10, 2009 at 8:3 Comment(3)
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? TksSarazen
@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.Linkage
nice but i do not understand this line var thread = new Thread( s => ((State)s).Result = Thread.CurrentThread.CurrentCulture); can u plzz explain.Croft
C
6

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

Chilpancingo answered 9/10, 2009 at 7:59 Comment(0)
D
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

Defaulter answered 7/11, 2013 at 1:57 Comment(2)
Similar to this, but I used GetUserDefaultLCID() instead which got me the user setting, I think the system default is the installed locale.Filter
[DllImport("kernel32.dll")] static extern int GetSystemDefaultLCID(); var name = new System.Globalization.CultureInfo(GetSystemDefaultLCID()).Name;Ezzell
D
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.

Dekameter answered 9/1, 2012 at 14:49 Comment(0)
M
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.

Monosome answered 28/12, 2013 at 16:58 Comment(0)
B
1
[DllImport("kernel32.dll")]
private static extern int GetUserDefaultLCID();

public static CultureInfo CurrentCultureInRegionalSettings => new CultureInfo(GetUserDefaultLCID());
Borchers answered 24/9, 2016 at 19:41 Comment(0)
F
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

Friary answered 9/10, 2009 at 7:59 Comment(0)
D
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;
Discompose answered 22/3, 2021 at 12:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.