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}.
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.
How does the .NET runtime determine the culture to use? Any detailed reference would be helpful.
This MSDN page on
CultureInfo
has some information that might be relevant:I think this might be a good starting point for your investigations.
In my case it took only one line of code to change the Culture:
It changes default Culture of Main thread and new ones as well.
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:
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 theirCurrentCulture
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.
since your problem seems to be related to calling
DateTime.ToString()
, make sure you pass the AU culture to theToString()
method:You could add this as an extension method to save you having to do this everywhere:
You can then call
DateTime.ToAUString()
to get the correct output.