I am trying to determine the current time zone for a Windows system. The following code snippet (free) is based on @MattJohnson's answer on this topic: Getting the local timezone identifier when the OS display language is not English
using (RegistryKey registryKey = Registry.LocalMachine.OpenSubKey( @"SYSTEM\CurrentControlSet\Control\TimeZoneInformation")) { if (registryKey != null) { string windowsTimeZoneId = registryKey.GetValue("TimeZoneKeyName") as string; if (string.IsNullOrEmpty(windowsTimeZoneId)) windowsTimeZoneId = registryKey.GetValue("StandardName") as string; if (!string.IsNullOrEmpty(windowsTimeZoneId)) { int i = windowsTimeZoneId.IndexOf('\0'); if (i != -1) windowsTimeZoneId = windowsTimeZoneId.Remove(i); return ConvertFromWindowsId(windowsTimeZoneId); } } }
If I put a breakpoint on the first line of "if (string.IsNullOrEmpty (windowsTimeZoneId)", then this is what I see in the Visual Studio debugger:

What's going on here? RegistryKey.GetValue () returns a string in a box, but why doesn't it detect two hex bytes and end the string there?
So far I have tested this on my two computers. This is the one that launches the 64-bit version of Windows 7. The other, running Windows 7 32-bit, is the same, except that it says that the length of the returned string is 128 characters instead of 127.
When viewing a registry entry using regedit.exe, this looks fine, showing only "Romantic Standard Time".
Googling raised this question a bit https://social.msdn.microsoft.com/Forums/sqlserver/en-US/eca7ad76-c910-46be-8fb9-876c7cde5c69/registry-read-time-zone-info-differ-when-code -build-on-40-framework-on-host-system-win-7-64-bit? forum = csharpgeneral Otherwise, I cannot find anything.
The program is built using Visual Studio 2012 and is aimed at .Net 2.0.
As you can see, I added the code to accept this βcorruptβ result, but still I would appreciate it if someone could explain to me what is happening here, and maybe even how to avoid the whole problem.
EDIT:
The program is built using Visual Studio 2012 and is aimed at .Net 2.0.
Hmm, the plot is thickening. This statement was not entirely correct. The stump of the code shown above is in the library assembly, which is built for the target .NET 2.0, but it is called from a program aimed at .NET 4.0. Now I tried calling it from a program that targets .Net 2.0, and there is no problem, RegistryKey.GetValue () just returns "Romance Standard Time". This seems to have something to do with .Net Framework 4.0, which is implied by the publication on MSDN.
EDIT 2 - Started Thinking It Is "Normal"
This is a little off topic for SO, but I would appreciate if someone would take a look at my related post on Superuser.com, as I don't get instant satisfaction there, and I would like it to resolve, so I know should i still panic or not. Thanks.
https://superuser.com/questions/859031/possible-malware-modification-of-windows-registry-entry