gpt4 book ai didi

c# - 当操作系统显示语言为非英语时获取本地时区标识符

转载 作者:行者123 更新时间:2023-11-30 17:03:07 29 4
gpt4 key购买 nike

奇怪的是,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 值包含键名,并且未本地化。

以上代码说明了这两种变体。

关于c# - 当操作系统显示语言为非英语时获取本地时区标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18950345/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com