.NET流程如何获取文化信息? [英] How does a .NET process get the culture information?

查看:110
本文介绍了.NET流程如何获取文化信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows Server上运行Windows服务(C#, .NET 2.0) 2003 R2。在一个服务器中, System.Threading.Thread.CurrentThread.CurrentCulture 是{en-AU},在另一个{en-US}中。
当在上调用ToString()时,这引起了不同DateTime 对象。我想要文化是{en-AU}。

I have a Windows service running (C#, .NET 2.0) on Windows Server 2003 R2. In one server the System.Threading.Thread.CurrentThread.CurrentCulture is {en-AU} and in the other {en-US}. This has caused a difference when calling ToString() on a DateTime object. I want the culture to be {en-AU}.

我查看了区域和语言环境。在两个服务器中,区域选项选项卡显示英文(Asutralia)。但在高级标签中,其中显示英语(美国),另一个显示英语(澳大利亚)。所以这一定是造成差异的。虽然我想知道为什么高级选项卡说您要使用的非Unicode编程程序的语言版本,但我认为.NET进程是Unicode,不应受到这种影响。

I checked the "Regional and Language Setting". In both servers, the "Regional Options" tab shows "English (Asutralia)". But in the "Advanced" tab it shows "English (United States)" for one and "English (Australia)" for the other. So this must be causing the difference. Although I want to know why exactly the "Advanced" tab says "the language version of the non-unicode programs you want to use", I thought .NET processes were Unicode and should not be affected by this.

.NET运行时如何确定要使用的文化?

How does the .NET runtime determine the culture to use? Any detailed reference would be helpful.

推荐答案

如果线程上没有设置文化,线程.CurrentThread.CurrentCulture 默认为用户默认文化 - 它从底层操作系统获取。这是由区域控制面板小程序中的格式部分确定的。

If a culture has not been set on the thread, Thread.CurrentThread.CurrentCulture defaults to the "user default culture" - which it gets from the underlying OS. This is determined by the Formats section in the regional control panel applet.

对于一项服务,默认情况下没有控制面板设置(对于上述情况)因为它运行在不具有配置文件的LocalSystem帐户下,因此它使用来自操作系统的系统区域设置。我不知道是否可以通过调整Windows中的设置为服务设置。

For a service, it has no control panel settings by default like for a user (the case above) as it runs under the LocalSystem account which will not have a profile, so it uses the system locale from the OS. I'm not sure if this can be set for a service by adjusting the settings in Windows.

您可以执行以下操作:


  1. 当服务启动时,您可以显式设置主线程的 CurrentCulture 。如果您这样做,您将需要牢记,在您的服务中创建的任何新线程还需要设置它们的 CurrentCulture ,因为线程不从父线程继承他们的文化。

  1. you can explicitly set the CurrentCulture of the main thread when the service starts. If you do this, you will need to bear in mind that any new threads that are created in your service will also need to have their CurrentCulture set as well, as threads do not inherit their culture from parent threads.

您可以将服务设置为以特定用户身份运行,并将该用户的区域设置(格式部分)设置为你想要使用的文化。

you can set the service to run as a specific user, and set that user's regional settings (the formats section) to be the culture you want to use. When the service starts as that use,it will use that user's regional settings.

因为您的问题似乎与调用 DateTime有关.ToString(),确保将AU文化传递给 ToString()方法:

since your problem seems to be related to calling DateTime.ToString(), make sure you pass the AU culture to the ToString() method:

DateTime.ToString(new CultureInfo("en-AU"))

您可以将其添加为扩展方法,以便您无处不在地执行此操作:

You could add this as an extension method to save you having to do this everywhere:

public static string ToAUString(this DateTime dateTime)
{
    return dateTime.ToString(new CultureInfo("en-AU"));
}

然后可以调用 DateTime.ToAUString() / code>以获得正确的输出。

You can then call DateTime.ToAUString() to get the correct output.

这篇关于.NET流程如何获取文化信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆