如何在 C# 中获取当前的区域设置?
通常,您可以通过编写类似
CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
的
内容来获取它,但是这样您只能获取在应用程序启动时配置的 CultureInfo,并且如果设置有,则不会更新后来被改变了。
那么,如何获取控制面板中当前配置的CultureInfo->区域和语言设置?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
正如 @Christian 提出的 ClearCachedData 是方法使用。但根据 MSDN:
因此您需要首先调用该函数,然后启动一个新线程。在这个新线程中,您可以使用 CurrentCulture 来获取文化的新值。
注意
,如果你还需要重置CurrentUICulture,你应该单独进行
As @Christian proposed ClearCachedData is the method to use. But according to MSDN:
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.
}
Note, that if you also need to reset CurrentUICulture, you should do it separately
Thread.CurrentThread.CurrentCulture.ClearCachedData()
看起来它会导致下次访问时重新读取区域性数据。Thread.CurrentThread.CurrentCulture.ClearCachedData()
looks like it will cause the culture data to be re-read when it is next accessed.您可以使用Win32 API函数GetSystemDefaultLCID。
其签名如下:
GetSystemDefaultLCID 函数返回LCID。它可以从下表映射语言字符串。
Microsoft 分配的区域设置 ID
You can use Win32 API function GetSystemDefaultLCID.
The signiture is as follow:
GetSystemDefaultLCID function returns the LCID. It can map language string from the folowing table.
Locale IDs Assigned by Microsoft
我们的 WinForms 应用程序遇到了这个问题,这是由于 Visual Studio 创建了 [MyApp].vshost.exe 进程,只要 Visual Studio 打开,该进程就始终在后台运行。
关闭MyApp ->属性->调试-> “启用 Visual Studio 托管进程”设置为我们解决了这个问题。
vshost 进程主要用于改进调试,但如果您不想禁用该设置,可以根据需要终止该进程。
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.
命名空间
System.Globalization
中有类CultureInfo
和TextInfo
。这两个类都有控制面板中定义的多个窗口区域设置。可用设置列表位于文档中。例如:
正在获取正在运行的程序的列表分隔符。
There are the classes
CultureInfo
andTextInfo
from the namespaceSystem.Globalization
. Both classes get several windows regional settings defined in the control panels. The list of available settings is in the documentation.For example:
is getting the list separator for the program which is running.
尝试在
SystemInformation
中找到您想要的设置类或使用
System.Management/System.Diagnostics
中的类查看 WMI,您可以使用 LINQ 到 WMI 也是如此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这个简单的代码对我有用(避免缓存):
This simple code worked for me (avoiding caching):