Example #1
0
        public override IResult Execute(IResult previousResults)
        {
            ProcessExtensions.TokenPrivileges? previousState = null;
            RegistryKey baseKey = null;
            try
            {
                baseKey = Registry.LocalMachine.OpenSubKey(Path.Combine(BaseRegistryPath, TimeZone), false);
                if (baseKey == null)
                    throw new Exception(string.Format("Cannot open registry store for provided timezone value ({0})", TimeZone));

                var info = GetDynamicTimeZoneInfo(baseKey);
                if (info == null)
                    throw new Exception(string.Format("Cannot retrieve time zone information from registry store for provided timezone value ({0})", TimeZone));

                var lastState = new ProcessExtensions.TokenPrivileges();
                if (!SetTimeZonePrivilege(ref lastState))
                    throw new Exception("Cannot set change time-zone privilege for current process");

                // put in nullable so we can tell whether we need to restore this in finally block
                previousState = lastState;

                var infoInstance = info.Value;
                if (!NativeMethods.SetDynamicTimeZoneInformation(ref infoInstance))
                    throw new Win32Exception();

                return new NextResult();
            }
            catch (Exception e)
            {
                Log.WarnFormat("An issue occurred while attempting to set this computer's time zone: {0}", e.Message);
                return new ExceptionOccurred(e);
            }
            finally
            {
                if (previousState.HasValue)
                    Process.GetCurrentProcess().SetPrivilegeTokens(previousState.Value);

                if (baseKey != null)
                    baseKey.Close();
            }
        }
Example #2
0
        public static void RestartComputer()
        {
            try
            {
                var lastState = new ProcessExtensions.TokenPrivileges();
                if (!Process.GetCurrentProcess().SetPrivilegeTokens("SeShutdownPrivilege", ref lastState))
                    throw new Exception("Could not restart computer: unable to set appropriate privileges for process");

                if (!NativeMethods.ExitWindowsEx(NativeMethods.ExitWindows.Reboot, 0))
                    throw new Win32Exception();

            }
            catch (Exception e)
            {
                Log.WarnFormat("An issue occurred while attempting to restart this computer: {0}", e.Message);
            }
        }