将 windows 服务 运行 的默认文化更改为 LocalSystem

Changing default culture for windows service running as LocalSystem

我们的管理员意外地迁移了运行我的 .NET windows 服务的服务器(迁移到 Windows 2008 ... 因为一些旧应用程序)。它是 运行 在 LocalSystem 帐户下。目标框架是 .NET framework 4 Client Profile。

在我的服务中,方法 DateTime.Parse 现在抛出无效格式异常。

当我添加到 OnStart

Thread.CurrentThread.CurrentCulture = new CultureInfo("cs-CZ", false);

它变得更好了,但在某些方法中它仍然失败,可能 运行 在不同的 therads 中。

如何配置服务器或更改程序,使所有线程都以正确的文化创建?

从 Microsoft.NET Framework 4.5 及更高版本开始,您可以使用

var culture = CultureInfo.CreateSpecificCulture("cs-CZ");
CultureInfo.DefaultThreadCurrentCulture = culture;

这是documented here, with the caveat that it might not work for subsequently created threads. A profound solution is included with Microsoft.NET Framework 4.6, read more on it here