public RegistryTimeZoneInformation(TimeZoneInformation tzi)
 {
     Bias = tzi.Bias;
     StandardDate = tzi.StandardDate;
     StandardBias = tzi.StandardBias;
     DaylightDate = tzi.DaylightDate;
     DaylightBias = tzi.DaylightBias;
 }
Пример #2
0
        public static void ChangeTimeZone(string newTimezone)
        {
            var regTimeZones = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones");

            string subKey = null;
            try
            {
                // Find Timezone in registry
                subKey = regTimeZones.GetSubKeyNames().Where(s => s == newTimezone).First();
            }
            catch (System.InvalidOperationException)
            {
                // List valid available timezones for this machine if
                // the timezone that was passed in was invalid.

                PrintValidtimeZones();
            }

            var daylightName = (string)regTimeZones.OpenSubKey(subKey).GetValue("Dlt");
            var standardName = (string)regTimeZones.OpenSubKey(subKey).GetValue("Std");
            var tzi = (byte[])regTimeZones.OpenSubKey(subKey).GetValue("TZI");

            var regTzi = new RegistryTimeZoneInformation(tzi);

            var tz = new TimeZoneInformation
            {
                Bias = regTzi.Bias,
                DaylightBias = regTzi.DaylightBias,
                StandardBias = regTzi.StandardBias,
                DaylightDate = regTzi.DaylightDate,
                StandardDate = regTzi.StandardDate,
                DaylightName = daylightName,
                StandardName = standardName
            };

            var enablePrivilage = TokenPrivilegesAccess.EnablePrivilege("SeTimeZonePrivilege");

            if (!enablePrivilage)
            {
                Console.WriteLine("Error: Could not enable privilege: SeTimeZonePrivilege");
                Environment.Exit(1);
            }

            var didSet = SetTimeZoneInformation(ref tz);
            var lastError = Marshal.GetLastWin32Error();
            TokenPrivilegesAccess.DisablePrivilege("SeTimeZonePrivilege");

            var key = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\TimeZoneInformation", true);
            if (key != null) key.SetValue("TimeZoneKeyName", key.GetValue("StandardName"));

            if (didSet)
            {
                Console.WriteLine("New Time Zone:{0}", TimeZoneInfo.Local.Id );
            }
            else
            {

                switch (lastError)
                {
                    case Program.ERROR_ACCESS_DENIED:
                        Console.WriteLine("Error: Access denied... Try running application as administrator.");
                        break;
                    case Program.CORSEC_E_MISSING_STRONGNAME:
                        Console.WriteLine("Error: Application is not signed ... Right click the project > Signing > Check 'Sign the assembly'.");
                        break;
                    default:
                        Console.WriteLine("Win32Error: " + lastError + "\nHRESULT: " + Marshal.GetHRForLastWin32Error());
                        break;
                }
            }
        }