public RegistryTimeZoneInformation(TimeZoneInformation tzi) { this.Bias = tzi.Bias; this.StandardDate = tzi.StandardDate; this.StandardBias = tzi.StandardBias; this.DaylightDate = tzi.DaylightDate; this.DaylightBias = tzi.DaylightBias; }
public static void Set(string name) { var regTimeZones = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones"); // Print out all the possible time-zones. //foreach(var subKey in regTimeZones.GetSubKeyNames()) //{ // Console.WriteLine(subKey); //} var subKey = regTimeZones.GetSubKeyNames().Where(s => s == name).First(); string daylightName = (string)regTimeZones.OpenSubKey(subKey).GetValue("Dlt"); string standardName = (string)regTimeZones.OpenSubKey(subKey).GetValue("Std"); byte[] tzi = (byte[])regTimeZones.OpenSubKey(subKey).GetValue("TZI"); var regTzi = new RegistryTimeZoneInformation(tzi); TokenPrivilegesAccess.EnablePrivilege("SeTimeZonePrivilege"); bool didSet; if (Environment.OSVersion.Version.Major < 6) { var tz = new TimeZoneInformation(); tz.Bias = regTzi.Bias; tz.DaylightBias = regTzi.DaylightBias; tz.StandardBias = regTzi.StandardBias; tz.DaylightDate = regTzi.DaylightDate; tz.StandardDate = regTzi.StandardDate; tz.DaylightName = daylightName; tz.StandardName = standardName; didSet = TimeZone.SetTimeZoneInformation(ref tz); } else { var tz = new DynamicTimeZoneInformation(); tz.Bias = regTzi.Bias; tz.DaylightBias = regTzi.DaylightBias; tz.StandardBias = regTzi.StandardBias; tz.DaylightDate = regTzi.DaylightDate; tz.StandardDate = regTzi.StandardDate; tz.DaylightName = daylightName; tz.StandardName = standardName; tz.TimeZoneKeyName = subKey; tz.DynamicDaylightTimeDisabled = false; didSet = TimeZone.SetDynamicTimeZoneInformation(ref tz); } int lastError = Marshal.GetLastWin32Error(); TokenPrivilegesAccess.DisablePrivilege("SeTimeZonePrivilege"); if (! didSet) { if (lastError == TimeZone.ERROR_ACCESS_DENIED) { throw new SecurityException("Access denied changing System Timezone."); } else if (lastError == TimeZone.CORSEC_E_MISSING_STRONGNAME) { throw new SystemException("Application is not signed."); } else { throw new SystemException("Win32Error: " + lastError + "\nHRESULT: " + Marshal.GetHRForLastWin32Error()); } } }
public static void Set(string name) { var regTimeZones = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones"); // Print out all the possible time-zones. //foreach(var subKey in regTimeZones.GetSubKeyNames()) //{ // Console.WriteLine(subKey); //} var subKey = regTimeZones.GetSubKeyNames().Where(s => s == name).First(); string daylightName = (string)regTimeZones.OpenSubKey(subKey).GetValue("Dlt"); string standardName = (string)regTimeZones.OpenSubKey(subKey).GetValue("Std"); byte[] tzi = (byte[])regTimeZones.OpenSubKey(subKey).GetValue("TZI"); var regTzi = new RegistryTimeZoneInformation(tzi); TokenPrivilegesAccess.EnablePrivilege("SeTimeZonePrivilege"); bool didSet; if (Environment.OSVersion.Version.Major < 6) { var tz = new TimeZoneInformation(); tz.Bias = regTzi.Bias; tz.DaylightBias = regTzi.DaylightBias; tz.StandardBias = regTzi.StandardBias; tz.DaylightDate = regTzi.DaylightDate; tz.StandardDate = regTzi.StandardDate; tz.DaylightName = daylightName; tz.StandardName = standardName; didSet = TimeZone.SetTimeZoneInformation(ref tz); } else { var tz = new DynamicTimeZoneInformation(); tz.Bias = regTzi.Bias; tz.DaylightBias = regTzi.DaylightBias; tz.StandardBias = regTzi.StandardBias; tz.DaylightDate = regTzi.DaylightDate; tz.StandardDate = regTzi.StandardDate; tz.DaylightName = daylightName; tz.StandardName = standardName; tz.TimeZoneKeyName = subKey; tz.DynamicDaylightTimeDisabled = false; didSet = TimeZone.SetDynamicTimeZoneInformation(ref tz); } int lastError = Marshal.GetLastWin32Error(); TokenPrivilegesAccess.DisablePrivilege("SeTimeZonePrivilege"); if (!didSet) { if (lastError == TimeZone.ERROR_ACCESS_DENIED) { throw new SecurityException("Access denied changing System Timezone."); } else if (lastError == TimeZone.CORSEC_E_MISSING_STRONGNAME) { throw new SystemException("Application is not signed."); } else { throw new SystemException("Win32Error: " + lastError + "\nHRESULT: " + Marshal.GetHRForLastWin32Error()); } } }
private static extern bool SetTimeZoneInformation([In] ref TimeZoneInformation lpTimeZoneInformation);