Getting the local timezone identifier when the OS display language is not English

Oddly, TimeZone.CurrentTimeZone.StandardName returns a localized name according to the display language on the computer. I want a programmatic identifier that I can provide TimeZoneInfo in the following code.

 TimeZoneInfo timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(timeZone); 

FindSystemTimeZoneById expects a unique non-localized program identifier

I changed the display language on the computer 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 the display language on the computer, which I do not want.

I have no way to use TimeZoneInfo.Local.Id right now because my project is in .Net 2.0. What other parameters do I need to get a non-localized time zone identifier?

+3
source share
1 answer

To get the equivalent of TimeZoneInfo.Local.Id , not being able to use the TimeZoneInfo class, you need to go directly to the registry.

In .NET 2.0 C #, you can get it using 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 above have a TimeZoneKeyName value and have a pointer @tzres.dll pointer in the value of StandardName .

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

The above code takes into account both options.

+2
source

Source: https://habr.com/ru/post/1210167/


All Articles