Getting local timezone identifier when OS display language is non-english

北城以北 提交于 2020-01-04 02:40:27

问题


Strangely, TimeZone.CurrentTimeZone.StandardName returns localized name as per computer display language. I want a programmatic identifier that I can provide to TimeZoneInfo in following code.

TimeZoneInfo timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(timeZone);

FindSystemTimeZoneById expects a unique un-localized programmatic identifier

I changed my computer display language to Chinese and I was getting a localized unicode string when I do TimeZone.CurrentTimeZone.StandardName. However the value was correct but it was localized in computer display language which is something I do not want.

I do not have option of using TimeZoneInfo.Local.Id right now because my project is in .Net 2.0. What other options do I have to get un-localized timezone identifier?


回答1:


To get the equivalent of TimeZoneInfo.Local.Id without being able to use the TimeZoneInfo class, you will have to go directly to the registry.

In .NET 2.0 C#, you can retrieve it with the following method:

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 and greater have the TimeZoneKeyName value, and have a @tzres.dll pointer entry in the StandardName value.

Before Windows Vista, the StandardName value contained the key name, and was not localized.

The above code accounts for both variations.



来源:https://stackoverflow.com/questions/18950345/getting-local-timezone-identifier-when-os-display-language-is-non-english

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!