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

发布于 2024-08-08 02:42:07 字数 227 浏览 4 评论 0原文

通常,您可以通过编写类似

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

瑶笙 2024-08-15 02:42:07

正如 @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()

As @Christian proposed ClearCachedData is the method to use. But according to MSDN:

The ClearCachedData method does not
refresh the information in the
Thread.CurrentCulture property for
existing threads

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
    }

}

Note, that if you also need to reset CurrentUICulture, you should do it separately

Thread.CurrentThread.CurrentUICulture.ClearCachedData()
海螺姑娘 2024-08-15 02:42:07

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.

揽月 2024-08-15 02:42:07

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

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

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

You can use Win32 API function GetSystemDefaultLCID.
The signiture is as follow:

[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

新人笑 2024-08-15 02:42:07

我们的 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.

有深☉意 2024-08-15 02:42:07

命名空间 System.Globalization 中有类 CultureInfoTextInfo。这两个类都有控制面板中定义的多个窗口区域设置。可用设置列表位于文档中。

例如:

string separator = CultureInfo.CurrentCulture.TextInfo.ListSeparator;

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

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.

For example:

string separator = CultureInfo.CurrentCulture.TextInfo.ListSeparator;

is getting the list separator for the program which is running.

篱下浅笙歌 2024-08-15 02:42:07
[DllImport("kernel32.dll")]
private static extern int GetUserDefaultLCID();

public static CultureInfo CurrentCultureInRegionalSettings => new CultureInfo(GetUserDefaultLCID());
[DllImport("kernel32.dll")]
private static extern int GetUserDefaultLCID();

public static CultureInfo CurrentCultureInRegionalSettings => new CultureInfo(GetUserDefaultLCID());
肤浅与狂妄 2024-08-15 02:42:07

尝试在 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

风情万种。 2024-08-15 02:42:07

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

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

This simple code worked for me (avoiding caching):

// 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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文