tags:

views:

1111

answers:

3

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?

+6  A: 

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

Christian Hayter
A: 

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

ArsenMkrt
+7  A: 

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
    }

}

Darin Dimitrov
Nice one Darin!
Christian Hayter
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
@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
Tks Darin... worked like a charm now!
Pascal