Windows Service Unable to get correct System Culture

放肆的年华 提交于 2019-11-30 20:01:45

问题


From Control Panel, I set my Region and Language setting to French (France)

When I am running my application as console application,

Thread.CurrentThread.CurrentCulture returns French

But when I'm running it as windows service, it returns invariant culture or English (US)

Is there a way to fix that?


回答1:


The service is probably running as a user that has it's own culture.

Why not set the culture when you start your service

Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-FR");

Also from Default Culture in a Windows Service

If your Windows Service is running under the SYSTEM account or other account without a 
profile it will use the settings defined under the 
"HKEY_USERS/.DEFAULT/Control Panel/International" registry key.  

You can change these values from "Control Panel / Regional and Language Options / Advanced" 
by checking the checkbox "Apply all settings to the current user account and to the default 
user profile".

I normally use the former technique as I only need to change it for a specific service rather than for the whole OS.




回答2:


.NET 4.5 added CultureInfo.DefaultThreadCurrentCulture and CultureInfo.DefaultThreadCurrentUICulture i suggest setting it as early as possible and it should solve your issue. This will change the current threads culture and all threads that are created.



来源:https://stackoverflow.com/questions/26474132/windows-service-unable-to-get-correct-system-culture

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