2

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

当我将我的应用程序作为控制台应用程序运行时,

Thread.CurrentThread.CurrentCulture返回法语

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

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

4

2 回答 2

9

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

为什么不在开始服务时设置文化

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

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

于 2014-10-20T20:24:35.917 回答
0

.NET 4.5添加了CultureInfo.DefaultThreadCurrentCultureCultureInfo.DefaultThreadCurrentUICulture我建议尽早设置它,它应该可以解决您的问题。这将改变当前线程文化和创建的所有线程。

于 2017-08-15T12:54:00.267 回答