tags:

views:

443

answers:

2

I have a windows service running (C#, .Net 2.0) on Windows 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}.

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 "Enlish (Australia)" for the other. So this must be causing the difference. Although I want to know why exactly as the "Advanced" tab says "the language version of the non-unicode programs you want o use". I thought .Net processes were Unicode and should not be affected by this.

My question is how does the .Net runtime determine the culture to use. Any detailed reference would be helpful.

+1  A: 

This MSDN page on CultureInfo has some information that might be relevant:

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. 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. 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.

I think this might be a good starting point for your investigations.

ChrisF
+1  A: 

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.

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.

There are a few things you could do:

  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.

  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(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.

adrianbanks
In this case the service (actually a WCF hosting service) is running under a specific service user account. But I have set the same account for the service in another machine for testing and it behaved correctly. Its only on the machine which has "English (US)" in the advanced tab the service is not behaving correctly.
Pratik
I have managed to log in as the service account in the machine and the format is "English (US)". So the user profile is incorrect. It is nothing to do with the Advanced tab. Thanks for pointing to right direction.
Pratik