从控制面板中,我将区域和语言设置为法语(法国)
当我以控制台应用程序的形式运行我的应用程序时,
Thread.CurrentThread.CurrentCulture
返回法语
但是当我以Windows服务的形式运行它时,它返回不变的文化或英语(美国)
有没有办法解决这个问题?
从控制面板中,我将区域和语言设置为法语(法国)
当我以控制台应用程序的形式运行我的应用程序时,
Thread.CurrentThread.CurrentCulture
返回法语
但是当我以Windows服务的形式运行它时,它返回不变的文化或英语(美国)
有没有办法解决这个问题?
该服务可能正在以拥有自己文化的用户身份运行。
为什么不在启动服务时设置文化呢?
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".
我通常使用前一种技术,因为我只需要为特定的服务更改它,而不是更改整个操作系统。
.NET 4.5
新增了CultureInfo.DefaultThreadCurrentCulture
和CultureInfo.DefaultThreadCurrentUICulture
,我建议尽早设置它,它应该可以解决您的问题。
这将更改当前线程的区域性以及所有创建的线程。