Windows服务无法获取正确的系统区域设置

4

从控制面板中,我将区域和语言设置为法语(法国)

当我以控制台应用程序的形式运行我的应用程序时,

Thread.CurrentThread.CurrentCulture返回法语

但是当我以Windows服务的形式运行它时,它返回不变的文化或英语(美国)

有没有办法解决这个问题?


这个问题有帮助吗:https://dev59.com/w3rZa4cB1Zd3GeqP7NBe - Preet Sangha
更改区域和语言设置后,您尝试重新启动计算机了吗? - RenniePet
2个回答

11

该服务可能正在以拥有自己文化的用户身份运行。

为什么不在启动服务时设置文化呢?

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

此外,还可以参考来自Windows服务中的默认文化的内容。

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".

我通常使用前一种技术,因为我只需要为特定的服务更改它,而不是更改整个操作系统。


感谢您发布解决方案。它对我很有帮助! - Sam

0

.NET 4.5新增了CultureInfo.DefaultThreadCurrentCultureCultureInfo.DefaultThreadCurrentUICulture,我建议尽早设置它,它应该可以解决您的问题。 这将更改当前线程的区域性以及所有创建的线程。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接