如何在C#中获取当前的区域设置?

26

通常可以通过编写以下代码获取当前区域设置:

CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;

但是这种方式仅能获取在应用程序启动时配置的CultureInfo,如果设置之后更改了,则不会更新。

那么,如何获取在“控制面板” -> “区域和语言设置”中配置的CultureInfo呢?

8个回答

31

正如 @Christian 建议的那样,ClearCachedData 是需要使用的方法。但是根据 MSDN 的说明:

对于现有线程,ClearCachedData 方法不会刷新 Thread.CurrentCulture 属性中的信息。

因此,您需要先调用该函数,然后启动一个新的线程。在这个新线程中,您可以使用 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
    }

请注意,如果你还需要重置CurrentUICulture,则应该单独进行操作。

Thread.CurrentThread.CurrentUICulture.ClearCachedData()

我遇到了错误:“找不到类型或命名空间名称'State'(是否缺少使用指令或程序集引用?)”,出现在以下代码行: var thread = new Thread( s => ((State)s).Result = Thread.CurrentThread.CurrentCulture); 问题出在对State的引用上。您有解决方法吗?谢谢。 - Pascal
@Pascal,“State”是我在“Program”类内定义的私有类,但您可以尝试将其外部化为自己的文件并使其公开。此外,“State”可能不是一个非常好的名称,因此您可以尝试将其重命名为更有意义的名称。 - Darin Dimitrov
很好,但我不理解这一行代码:var thread = new Thread( s => ((State)s).Result = Thread.CurrentThread.CurrentCulture); 你能解释一下吗? - Thomas

6

Thread.CurrentThread.CurrentCulture.ClearCachedData() 看起来会导致当下次访问时,文化数据将被重新读取。


3
您可以使用Win32 API函数GetSystemDefaultLCID。 其签名如下:
[DllImport("kernel32.dll")]
static extern uint GetSystemDefaultLCID();

GetSystemDefaultLCID函数返回LCID。它可以从以下表中映射语言字符串。 Microsoft分配的区域设置ID


类似于这个,但我使用了GetUserDefaultLCID()代替,这给我带来了用户设置,我认为系统默认是安装的区域设置。 - Colin
[DllImport("kernel32.dll")] static extern int GetSystemDefaultLCID();var name = new System.Globalization.CultureInfo(GetSystemDefaultLCID()).Name; - Giles Bathgate

2

我们的WinForms应用程序遇到了这个问题,原因是Visual Studio创建了[MyApp].vshost.exe进程,该进程始终在后台运行,只要Visual Studio打开。

关闭 MyApp -> Properties -> Debug -> "启用Visual Studio托管进程"选项可以解决此问题。

vshost进程主要用于改善调试功能,但如果不想禁用该设置,您可以根据需要结束该进程。


1

有两个类CultureInfoTextInfo,它们来自命名空间System.Globalization。这两个类都获取了控制面板中定义的几个Windows区域设置。可用设置列表在文档中。

例如:

string separator = CultureInfo.CurrentCulture.TextInfo.ListSeparator;

获取运行程序的列表分隔符。

1
[DllImport("kernel32.dll")]
private static extern int GetUserDefaultLCID();

public static CultureInfo CurrentCultureInRegionalSettings => new CultureInfo(GetUserDefaultLCID());

0

这段简单的代码对我有效(避免缓存):

// 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;

0

尝试在SystemInformation类中查找所需的设置,或者使用System.Management/System.Diagnostics中的类来查看WMI,您也可以使用LINQ to WMI


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接