简体   繁体   English

.NET进程如何获取文化信息?

[英]How does a .NET process get the culture information?

I have a Windows service running (C#, .NET 2.0) on Windows Server 2003 R2. 我在Windows Server 2003 R2上运行了一个Windows服务(C# ,. NET 2.0)。 In one server the System.Threading.Thread.CurrentThread.CurrentCulture is {en-AU} and in the other {en-US}. 在一台服务器中, System.Threading.Thread.CurrentThread.CurrentCulture为{en-AU},另一台为{en-US}。 This has caused a difference when calling ToString() on a DateTime object. 这在DateTime对象上调用ToString()时会产生差异。 I want the culture to be {en-AU}. 我希望文化是{en-AU}。

I checked the "Regional and Language Setting". 我检查了“区域和语言设置”。 In both servers, the "Regional Options" tab shows "English (Asutralia)". 在两个服务器中,“区域选项”选项卡显示“英语(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. 虽然我想知道为什么“高级”选项卡上写着“你想要使用的非unicode程序的语言版本”,但我认为.NET进程是Unicode,不应受此影响。

How does the .NET runtime determine the culture to use? .NET运行时如何确定要使用的文化? Any detailed reference would be helpful. 任何详细的参考都会有所帮助。

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. 如果尚未在线程上设置文化,则Thread.CurrentThread.CurrentCulture默认为“用户默认文化” - 它从底层操作系统获取。 This is determined by the Formats section in the regional control panel applet. 这由区域控制面板小程序中的“格式”部分确定。

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. 对于服务,它默认没有控制面板设置,例如用户(上述情况),因为它在没有配置文件的LocalSystem帐户下运行,因此它使用操作系统中的系统区域设置。 I'm not sure if this can be set for a service by adjusting the settings in Windows. 我不确定是否可以通过调整Windows中的设置来为服务设置此项。

There are a few things you could do: 你可以做一些事情:

  1. you can explicitly set the CurrentCulture of the main thread when the service starts. 您可以在服务启动时显式设置主线程的CurrentCulture 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. 如果这样做,您将需要记住,在您的服务中创建的任何新线程也需要设置其CurrentCulture ,因为线程不会从父线程继承其文化。

  2. 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. 当服务以该用途开始时,它将使用该用户的区域设置。

  3. since your problem seems to be related to calling DateTime.ToString() , make sure you pass the AU culture to the ToString() method: 因为您的问题似乎与调用DateTime.ToString() ,请确保将AU文化传递给ToString()方法:

     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")); } 

    You can then call DateTime.ToAUString() to get the correct output. 然后,您可以调用DateTime.ToAUString()来获取正确的输出。

In my case it took only one line of code to change the Culture: 在我的例子中,只需要一行代码来改变文化:

System.Globalization.CultureInfo.DefaultThreadCurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo( "en-US" )

It changes default Culture of Main thread and new ones as well. 它改变了主线程的默认文化和新线程。

This MSDN page on CultureInfo has some information that might be relevant: CultureInfo上的这个MSDN页面有一些可能相关的信息:

The user might choose to override some of the values associated with the current culture of Windows through the regional and language options portion of Control Panel. 用户可以选择通过“控制面板”的区域和语言选项部分覆盖与当前Windows文化关联的某些值。 For example, the user might choose to display the date in a different format or to use a currency other than the default for the culture. 例如,用户可能选择以不同的格式显示日期,或者使用除文化默认值以外的货币。 In general, your applications should honor these user overrides. 通常,您的应用程序应该遵守这些用户覆盖。

If UseUserOverride is true and the specified culture matches the current culture of Windows, the CultureInfo uses those overrides, including user settings for the properties of the DateTimeFormatInfo instance returned by the DateTimeFormat property, and the properties of the NumberFormatInfo instance returned by the NumberFormat property. 如果UseUserOverride为true且指定的区域性与Windows的当前区域性匹配,则CultureInfo将使用这些覆盖,包括DateTimeFormat属性返回的DateTimeFormatInfo实例的属性的用户设置,以及NumberFormat属性返回的NumberFormatInfo实例的属性。 If the user settings are incompatible with the culture associated with the CultureInfo, for example, if the selected calendar is not one of the OptionalCalendars, the results of the methods and the values of the properties are undefined. 如果用户设置与CultureInfo关联的区域性不兼容,例如,如果所选日历不是OptionalCalendars之一,则方法的结果和属性的值是未定义的。

I think this might be a good starting point for your investigations. 我认为这可能是你调查的一个很好的起点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM