操作系统显示语言为非英语时获取本地时区标识符
奇怪的是, TimeZone.CurrentTimeZone.StandardName
按照计算机显示语言返回本地化名称。 我想要一个程序化标识符,我可以在下面的代码中提供给TimeZoneInfo
。
TimeZoneInfo timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(timeZone);
FindSystemTimeZoneById
需要一个唯一的非本地化程序标识符
我将计算机显示语言改为中文,当我执行TimeZone.CurrentTimeZone.StandardName
时,我得到了一个本地化的unicode字符串。 然而,价值是正确的,但它是本地化的计算机显示语言,这是我不想要的。
我现在没有使用TimeZoneInfo.Local.Id
选项,因为我的项目是在.Net 2.0中。 我还有哪些其他选项可以获得非本地化的时区标识符?
要在不能使用TimeZoneInfo
类的情况下获得等效的TimeZoneInfo.Local.Id
,您必须直接进入注册表。
在.NET 2.0 C#中,您可以使用以下方法检索它:
private static string GetLocalTimeZoneId() { RegistryKey key = Registry.LocalMachine.OpenSubKey( @"SYSTEM\CurrentControlSet\Control\TimeZoneInformation"); string value = (string)key.GetValue("TimeZoneKeyName"); if (string.IsNullOrEmpty(value)) value = (string)key.GetValue("StandardName"); key.Close(); return value; }
Windows Vista和更高版具有TimeZoneKeyName
值,并在StandardName
值中具有@tzres.dll
指针条目。
在Windows Vista之前, StandardName
值包含密钥名称,并且未本地化。
上面的代码说明了这两种变化。