private static extern bool SetDynamicTimeZoneInformation([In] ref DynamicTimeZoneInformation lpTimeZoneInformation);
public static void Main(string[] args) { 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 == "Atlantic Standard Time").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 = Program.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 = Program.SetDynamicTimeZoneInformation(ref tz); } int lastError = Marshal.GetLastWin32Error(); TokenPrivilegesAccess.DisablePrivilege("SeTimeZonePrivilege"); if (didSet) { Console.WriteLine("Success, TimeZone Set!"); } else { if (lastError == Program.ERROR_ACCESS_DENIED) { Console.WriteLine("Error: Access denied... Try running application as administrator."); } else if (lastError == Program.CORSEC_E_MISSING_STRONGNAME) { Console.WriteLine("Error: Application is not signed ... Right click the project > Signing > Check 'Sign the assembly'."); } else { Console.WriteLine("Win32Error: " + lastError + "\nHRESULT: " + Marshal.GetHRForLastWin32Error()); } } Console.ReadLine(); }
public static void ChangeTimeZone(int timeZoneOffset, bool refreshLive) { var regTimeZones = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones"); // Print out all the possible time-zones. foreach (var sKey in regTimeZones.GetSubKeyNames()) { Console.WriteLine(sKey); } string timeZoneName = timeZoneNames[timeZoneOffset]; var subKey = regTimeZones.GetSubKeyNames().Where(s => s == timeZoneName).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 = TimeZoneControl.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 = TimeZoneControl.SetDynamicTimeZoneInformation(ref tz); } int lastError = Marshal.GetLastWin32Error(); TokenPrivilegesAccess.DisablePrivilege("SeTimeZonePrivilege"); if (didSet) { Console.WriteLine("Success, TimeZone Set!"); } else { if (lastError == TimeZoneControl.ERROR_ACCESS_DENIED) { //Console.WriteLine("Error: Access denied... Try running application as administrator."); } else if (lastError == TimeZoneControl.CORSEC_E_MISSING_STRONGNAME) { //Console.WriteLine("Error: Application is not signed ... Right click the project > Signing > Check 'Sign the assembly'."); } else { //Console.WriteLine("Win32Error: " + lastError + "\nHRESULT: " + Marshal.GetHRForLastWin32Error()); } } TimeZoneInfo.ClearCachedData(); if (refreshLive) { if (Mediator != null) { Mediator.SendMessage(true, MsgTag.Refresh); } } //Console.ReadLine(); }