如何获得在C#中当前的区域设置? [英] How to get current regional settings in C#?
问题描述
通常情况下,你可以通过编写像
Normally you can get it by writing something like
的CultureInfo的CurrentCulture = Thread.CurrentThread.CurrentCulture;
不过这样一来,你只能得到的CultureInfo这是目前应用程序的启动配置,如果设置已经改变之后将不会更新。
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.
那么,如何让当前的CultureInfo控制面板中的配置 - >区域和语言设置
So, how to get CultureInfo currently configured in Control Panel -> Regional and Language Settings?
推荐答案
由于@Christian提出的 ClearCachedData a>是使用的方法。但根据MSDN:
As @Christian proposed ClearCachedData is the method to use. But according to MSDN:
该ClearCachedData方法不
刷新的信息
Thread.CurrentCulture属性
现有线程
The ClearCachedData method does not refresh the information in the Thread.CurrentCulture property for existing threads
所以,你需要先调用的函数,然后开始一个新的线程。在这个新的线程可以使用的CurrentCulture获得文化的新鲜值。
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
}
}
请注意,如果您还需要重置的CurrentUICulture,您需要单独做
Note, that if you also need to reset CurrentUICulture, you should do it separately
Thread.CurrentThread.CurrentUICulture.ClearCachedData()
这篇关于如何获得在C#中当前的区域设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!