如何在 C# 中获取当前区域设置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1542409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get current regional settings in C#?
提问by Shuric
Normally you can get it by writing something like
通常你可以通过编写类似的东西来获得它
CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
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.
但是这样您只能获取在应用程序启动时配置的 CultureInfo,如果之后更改了设置,则不会更新。
So, how to get CultureInfo currently configured in Control Panel -> Regional and Language Settings?
那么,如何获取当前在控制面板 -> 区域和语言设置中配置的 CultureInfo?
采纳答案by Darin Dimitrov
As @Christian proposed ClearCachedDatais the method to use. But according to MSDN:
正如@Christian 提出的ClearCachedData是要使用的方法。但根据 MSDN:
The ClearCachedData method does not refresh the information in the Thread.CurrentCulture property for existing threads
ClearCachedData 方法不会刷新现有线程的 Thread.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.
因此,您需要先调用该函数,然后再启动一个新线程。在这个新线程中,您可以使用 CurrentCulture 获取文化的新值。
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
请注意,如果您还需要重置 CurrentUICulture,则应单独进行
Thread.CurrentThread.CurrentUICulture.ClearCachedData()
回答by Christian Hayter
Thread.CurrentThread.CurrentCulture.ClearCachedData()
looks like it will cause the culture data to be re-read when it is next accessed.
Thread.CurrentThread.CurrentCulture.ClearCachedData()
看起来它会导致在下次访问时重新读取文化数据。
回答by Arsen Mkrtchyan
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 WMItoo
试着找到你想要的设置中SystemInformation
使用的类类或可考虑WMI System.Management/System.Diagnostics
,你可以使用LINQ到WMI太
回答by jHymana
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.
我们在使用 WinForms 应用程序时遇到了这个问题,这是由于 Visual Studio 创建了 [MyApp].vshost.exe 进程,只要 Visual Studio 打开,该进程总是在后台运行。
Turning off the MyApp -> Properties -> Debug -> "Enable Visual Studio hosting process" setting fixed this for us.
关闭 MyApp -> 属性 -> 调试 ->“启用 Visual Studio 托管进程”设置为我们解决了这个问题。
The vshostprocess is mainly used to improve debugging, but if you don't want disable the setting, you can kill the process as needed.
该vshost工艺主要用于改善调试,但如果你不希望禁用的设置,您可以根据需要终止进程。
回答by Mark Green
You can use Win32 API function GetSystemDefaultLCID. The signiture is as follow:
您可以使用 Win32 API 函数 GetSystemDefaultLCID。签名如下:
[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
GetSystemDefaultLCID 函数返回 LCID。它可以映射下表中的语言字符串。 Microsoft 分配的区域设置 ID
回答by Nicolas
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.
有类CultureInfo
和TextInfo
来自命名空间System.Globalization
。这两个类都获得了在控制面板中定义的几个窗口区域设置。可用设置列表在文档中。
For example:
例如:
string separator = CultureInfo.CurrentCulture.TextInfo.ListSeparator;
is getting the list separator for the program which is running.
正在获取正在运行的程序的列表分隔符。
回答by tjsmith
[DllImport("kernel32.dll")]
private static extern int GetUserDefaultLCID();
public static CultureInfo CurrentCultureInRegionalSettings => new CultureInfo(GetUserDefaultLCID());