public static List<DisplayTimeZone> GetSystemTimeZones()
{
List<DisplayTimeZone> list = new List<DisplayTimeZone>();
PermissionSet set = new PermissionSet(PermissionState.None);
set.AddPermission(new RegistryPermission(RegistryPermissionAccess.Read, @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones"));
set.Assert();
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones", RegistryKeyPermissionCheck.Default, RegistryRights.ExecuteKey))
{
if (key != null)
{
foreach (string str in key.GetSubKeyNames())
{
DisplayTimeZone timeZone = null;
Exception e = null;
TryGetTimeZone(str, out timeZone, out e);
if (timeZone != null)
{
list.Add(timeZone);
}
}
}
}
return list;
}
private static void TryGetTimeZone(string id, out DisplayTimeZone value, out Exception e)
{
e = null;
value = new DisplayTimeZone();
try
{
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(string.Format(CultureInfo.InvariantCulture, @"{0}\{1}", new object[] { @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones", id }), RegistryKeyPermissionCheck.Default, RegistryRights.ExecuteKey))
{
value.DisplayName = key.GetValue("Display", string.Empty, RegistryValueOptions.None) as string;
value.StandardName = key.GetValue("Std", string.Empty, RegistryValueOptions.None) as string;
value.DaylightName = key.GetValue("Dlt", string.Empty, RegistryValueOptions.None) as string;
}
}
catch (Exception ex)
{
e = ex;
value = null;
}
}
public class DisplayTimeZone
{
public string StandardName;
public string DisplayName;
public string DaylightName;
}