Server set to en-GB in Regional settings but DateTime parsing as en-US

谁说我不能喝 提交于 2019-12-12 11:54:42

问题


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)

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?


回答1:


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

  • MSDN - Thread.CurrentCulture Property

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.




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/9132720/server-set-to-en-gb-in-regional-settings-but-datetime-parsing-as-en-us

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!