Question

I am processing records by pushing each record through validation stages, then into a database. One of the validation steps requires checking if certain columns are dates. I did this using DateTime.TryParse(s, out DateTime) assuming that this would use the configured Regional Settings on the machine on which the process was running. On my local machine, this is a wrapper class running within a command-line harness from Visual Studio (for ease of debugging). So 13/01/2010 gets formatted as 13 January 2010 as per the en-GB setting on my Windows 7 dev machine.

On pushing this onto our test server, a Windows Server 2008 R2, this wrapper class runs within a Window Service (under the LocalSystem account). Exactly the same code, I've designed it such that the service is just a thin wrapper. However, after many cycles of debugging, it seems the server is parsing 13/01/2010 as en-US and therefore failing. This is despite the Regional Settings being set to en-GB. (See screenshot)

Regional Settings on server

Note that this is way before it gets to SQL Server so that is not part of this issue.

After fighting with this, I've forced the situation by using the code below and setting the required format to en-GB.

Culture = CultureInfo.CreateSpecificCulture("en-GB");
DateTimeStyles dateTimeStyles = DateTimeStyles.None;
DateTime dt;
bool pass = DateTime.TryParse(s,Culture,dateTimeStyles, out dt);

This now works. My question is, how come the Windows Service, running under Local System assumes en-US and not en-GB?

Was it helpful?

Solution

Description

You can set the culture of the current thread to this using Thread.CurrentThread.CurrentCulture. This will have effect on all Culture specific things in the current thread.

Sample

Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-GB");

More Information

Update

It seems that your regional settings associated with your account profile are irrelevant when the service is running with the SYSTEM account. The default Culture for a windows service is en-US. There are many discussions on the web. All ending in "set the culture of the CurrentThread", like my answer.

OTHER TIPS

Resolved for me by setting the .net Globalization parameter "Set UI Culture".

On Windows 2008 server:

  • Load IIS manager

  • Go to the website

  • .net Globalization

  • Set UI Culture to "English (United Kingdom) (en-GB)"

Thanks

I have ran into this problem some days ago, after awhile I realize that my Application Pool Identity setting was set to Local system account, I changed it to you use custom account (Administator), so I don't need to set the culture of the current thread.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top