Machine Culture and DateTime

2019-09-08 05:23发布

问题:

We set up a server wit the en-US culture by mistake. Later I changed the regional settings, the locale, and the IIS globalisation to en-GB.

However, I'm having a problem with a Windows service I've written. It's still using the US format whenever DateTime.ToString() is done. I know I can specify the culture I want the string to be in, but I don't want to change all my code. It should just use the UK format because it's changed everywhere in the machine settings.

Maybe there's a setting I've missed? Does anyone have any suggestions apart from changing the code?

回答1:

When you change the regional settings using Control Panel, you need to check the checkbox "Apply all settings to the current user account and to the default user profile". In XP this is on the Advanced tab of Regional and Language Options.

Your Windows Service is presumably running under an account without a profile (e.g. LocalSystem) so will use the default user profile settings.

You can also inspect the regional settings for the default user profile using RegEdit - they're under the registry key HKEY_USERS\.DEFAULT\Control Panel\International.

Nevertheless I would consider changing the code - a Windows Service should generally be agnostic about the machine's culture, and you'll be avoiding maintenance headaches down the line.



回答2:

if your windows service is running on a system account you need to apply the correct (en-GB) language settings also to reserved accounts