如何做一个.NET过程中获得的文化信息? [英] How does a .NET process get the culture information?

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

问题描述

我有运行Windows服务(C#, .NET 2.0),Windows Server 2003上R2。在一个服务器中的 System.Threading.Thread.CurrentThread.CurrentCulture 是{EN-AU}和{其他EN-US}。 这造成了不同的调用toString()在日期时间目的。我想文化是{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)。但是,在高级选项卡中显示英语(美国)一和英语(澳大利亚)为其他。因此必须使之差。虽然我想知道到底为什么高级选项卡中说:您要使用非UNI code程序的语言版本,我以为.NET过程是统一code,不应受这一点。

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.

推荐答案

如果一个文化并没有被设置的线程上, Thread.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(),一定要通过非盟文化走向的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(),以获得正确的输出。

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

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

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