Beispiel #1
0
        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 == "Eastern 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);

            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;

            TokenPrivilegesAccess.EnablePrivilege("SeTimeZonePrivilege");
            Program.SetTimeZoneInformation(ref tz);
            int lastError = Marshal.GetLastWin32Error();

            TokenPrivilegesAccess.DisablePrivilege("SeTimeZonePrivilege");

            if (lastError == 0)
            {
                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();
        }
Beispiel #2
0
        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);

            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;

            TokenPrivilegesAccess.EnablePrivilege("SeTimeZonePrivilege");
            bool didSet    = Program.SetTimeZoneInformation(ref tz);
            int  lastError = Marshal.GetLastWin32Error();

            TokenPrivilegesAccess.DisablePrivilege("SeTimeZonePrivilege");

            // NOTE: This fixes the "Your current time zone is not recognized. Please select a valid time zone using the link below" error
            //       only when the machine is *restarted*.
            //
            //       You must have the following set in your app.manifest to request admin rights to write to this key.
            //       <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
            //          <security>
            //          <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
            //              <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
            //          </requestedPrivileges>
            //      ...
            var key = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\TimeZoneInformation", true);

            key.SetValue("TimeZoneKeyName", key.GetValue("StandardName"));

            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()
        {
            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 == "W. Europe 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 = 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());
                }
            }

            //Console.ReadLine();
        }