The server is set to en-GB in regional settings, but DateTime parsing is like en-US

I process the records by clicking each record through the verification steps, and then into the database. One of the verification steps is to check whether 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 on the command line from Visual Studio (to facilitate debugging). Thus, 01/13/2010 is formatted as January 13, 2010 according to the en-GB setting on my Windows 7 computer.

By clicking this on our test server, Windows Server 2008 R2, this wrapper class runs in the Windows service (under the LocalSystem account). Exactly the same code, I designed it in such a way that the service was just a thin shell. However, after many debugging cycles, it seems that the server is parsing 01/13/2010 as en-US and therefore fails. This is despite the fact that the regional settings are set to en-GB. (See screenshot)

Regional Settings on server

Note that this is the path before it gets to SQL Server, so this is not part of this problem.

After dealing with this, I made the situation 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); 

Now it works. My question is: how did it happen that a Windows service running on a local system accepts en-US, not en-GB?

+4
source share
3 answers

Description

You can set the culture of the current thread using Thread.CurrentThread.CurrentCulture . This will affect all cultural-related things in the current topic.

Example

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

Additional Information

Update

It seems that your regional settings associated with your account profile do not matter when the service is running with a SYSTEM account. The default service for Windows is en-US . There are a lot of discussions on the Internet. It all ends with "set the culture of CurrentThread", like my answer.

+2
source

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

On a Windows 2008 server:

  • Download IIS Manager

  • Go to website

  • .net globalization

  • Set the user interface culture to "English (UK) (en-GB)"

thanks

+2
source

I ran into this problem a few days ago, after a while I realized that the "Application Pool Identifier" parameter was set to "Local System Account", I changed it to use a user account (Administator), so I do not need to set the culture current thread.

+1
source

Source: https://habr.com/ru/post/1394660/


All Articles