public static string GetOperationName(this ShutDownOperation op, bool force)
        {
            string opName;

            switch (op)
            {
            case ShutDownOperation.Hibernate:
                opName = "Hibernate";
                break;

            case ShutDownOperation.Restart:
                opName = force ? "Forced restart" : "Restart";
                break;

            case ShutDownOperation.ShutDown:
                opName = force ? "Forced shut down" : "Shut down";
                break;

            case ShutDownOperation.SignOut:
                opName = "Log off";
                break;

            case ShutDownOperation.Sleep:
                opName = "Sleep";
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
            return(opName);
        }
        public static void ExecuteShutDownOperation(ShutDownOperation operation, bool force)
        {
            try
            {
                string cmd  = @"shutdown";
                string args = operation.GetCommandLineArgs(force);

                var start = new ProcessStartInfo(cmd, args);
                start.ErrorDialog = true;
                Debug.WriteLine("cmd: " + (cmd ?? "null"));
                Debug.WriteLine("args: " + (args ?? "null"));
                start.UseShellExecute = false;
                start.CreateNoWindow  = true;
                using (Process proc = Process.Start(start))
                {
                    proc.WaitForExit();
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                string operationName = force ? "forced " : "not forced ";
                operationName += operation.ToString().ToLower();
                Log.LogErrorAndDisplayMessageBox($"Failed to execute {operation}", ex);
            }
        }
Exemple #3
0
        public static string GetOperationName(this ShutDownOperation op, bool force, bool useShortName = false)
        {
            string opName;

            switch (op)
            {
            case ShutDownOperation.Hibernate:
                opName = useShortName ? "H" : "Hibernate";
                break;

            case ShutDownOperation.Restart:
                if (useShortName)
                {
                    opName = force ? "F-R" : "R";
                }
                else
                {
                    opName = force ? "Forced restart" : "Restart";
                }
                break;

            case ShutDownOperation.ShutDown:
                if (useShortName)
                {
                    opName = force ? "F-SD" : "SD";
                }
                else
                {
                    opName = force ? "Forced shut down" : "Shut down";
                }
                break;

            case ShutDownOperation.SignOut:
                opName = useShortName ? "LO" : "Log off";
                break;

            case ShutDownOperation.Sleep:
                opName = useShortName ? "S" : "Sleep";
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
            return(opName);
        }
Exemple #4
0
        public void Execute(ShutDownOperation operation, bool force)
        {
            string cmd  = @"shutdown";
            string args = operation.GetCommandLineArgs(force);

            var start = new ProcessStartInfo(cmd, args)
            {
                ErrorDialog = true
            };

            Debug.WriteLine("cmd: " + (cmd ?? "null"));
            Debug.WriteLine("args: " + (args ?? "null"));
            start.UseShellExecute = false;
            start.CreateNoWindow  = true;
            using (Process proc = Process.Start(start))
            {
                proc?.WaitForExit();
            }
        }
Exemple #5
0
        public void Execute(ShutDownOperation operation, bool force)
        {
            PowerState standbyPowerState;

            switch (operation)
            {
            case ShutDownOperation.Sleep:
                standbyPowerState = PowerState.Suspend;
                break;

            case ShutDownOperation.Hibernate:
                standbyPowerState = PowerState.Hibernate;
                break;

            default:
                throw new InvalidOperationException("Only sleep and hibernate standby operations are supported.");
            }

            Application.SetSuspendState(standbyPowerState, force, false);
        }
Exemple #6
0
 public void ModifyMachineState(ShutDownOperation operation, bool force)
 {
     try
     {
         //The shutdown command does not support sleep.  Use the psshutdown command instead
         if (operation != ShutDownOperation.Sleep && operation != ShutDownOperation.Hibernate)
         {
             _shutdownExecutor.Execute(operation, force);
         }
         else
         {
             _standbyExecutor.Execute(operation, force);
         }
     }
     catch (Exception ex)
     {
         Debug.WriteLine(ex.Message);
         string operationName = force ? "forced " : "not forced ";
         operationName += operation.ToString().ToLower();
         Log.LogErrorAndDisplayMessageBox($"Failed to execute {operation}", ex);
     }
 }
        public static string GetCommandLineArgs(this ShutDownOperation operation, bool force)
        {
            string args;

            switch (operation)
            {
            case ShutDownOperation.Hibernate:
                args = string.Empty;
                break;

            case ShutDownOperation.Restart:
                args = "-r";
                break;

            case ShutDownOperation.ShutDown:
                args = "-s";
                break;

            case ShutDownOperation.SignOut:
                args = "-l";
                break;

            case ShutDownOperation.Sleep:
                args = string.Empty;
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            if (!string.IsNullOrEmpty(args))
            {
                args += (force ? " -f" : string.Empty) + " -t 0";
            }

            return(args);
        }