26

通常你可以通过写类似的东西来获得它

CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;

但是这样您只能获取在应用程序启动时配置的 CultureInfo,如果之后更改设置,则不会更新。

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

4

8 回答 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()
于 2009-10-09T08:03:18.703 回答
6

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

于 2009-10-09T07:59:19.003 回答
3

您可以使用 Win32 API 函数 GetSystemDefaultLCID。签名如下:

[DllImport("kernel32.dll")]
static extern uint GetSystemDefaultLCID();

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

于 2013-11-07T01:57:00.327 回答
2

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

关闭 MyApp -> Properties -> Debug -> "Enable Visual Studio hosting process" 设置为我们解决了这个问题。

vshost进程主要用于改善调试,但如果不想禁用该设置,可以根据需要将进程kill掉。

于 2012-01-09T14:49:45.763 回答
1

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

例如:

string separator = CultureInfo.CurrentCulture.TextInfo.ListSeparator;

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

于 2013-12-28T16:58:01.917 回答
1
[DllImport("kernel32.dll")]
private static extern int GetUserDefaultLCID();

public static CultureInfo CurrentCultureInRegionalSettings => new CultureInfo(GetUserDefaultLCID());
于 2016-09-24T19:41:20.557 回答
0

尝试在课堂上找到您想要的设置SystemInformation 或使用 中的类查看 WMI ,您也System.Management/System.Diagnostics可以使用LINQ to WMI

于 2009-10-09T07:59:38.457 回答
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;
于 2021-03-22T12:42:18.170 回答