Esempio n. 1
0
        public static void RestartSystem()
        {
            var cmdStr = "shutdown /r";
            var cmd    = new ShellCommand(cmdStr);

            if (!cmd.TryExecute(out _))
            {
                Log.Error("SystemUtils.RestartSystem", "Unable to perform PC restart");
            }
        }
Esempio n. 2
0
        public static void DeleteResumeTask()
        {
            var schCmd = $"schtasks.exe /delete /tn \"{RESTART_SHTASK_NAME}\" /f";
            var cmd    = new ShellCommand(schCmd);

            if (!cmd.TryExecute(out _))
            {
                //Log.Error("SystemUtils.DeleteResumeTask", "Unable to delete resume task");
            }

            File.Delete("restart.bat");
        }
Esempio n. 3
0
        public static void ScheduleAfterRestart()
        {
            DeleteResumeTask();
            var myExePath            = Assembly.GetEntryAssembly().Location;
            var workDirPath          = Path.GetDirectoryName(myExePath);
            var psExecExecutable     = Environment.Is64BitOperatingSystem ? "PsExec64.exe" : "PsExec.exe";
            var psExecPath           = Path.GetFullPath(Path.Combine(Configs.TOOLS_DIR, "PsExec", psExecExecutable));
            var restartCmd           = $"\"{psExecPath}\" -d -accepteula -i 1 -w \"{workDirPath}\" \"{myExePath}\" /resume";
            var restartBatchFilePath = Path.GetFullPath("restart.bat");

            File.WriteAllText(restartBatchFilePath, restartCmd);
            //an alternative to task scheduler could be Run/ RunOnce registy keys
            //var schCmd = $"schtasks.exe /create /tn \"{RESTART_SHTASK_NAME}\" /ru SYSTEM /sc ONLOGON /tr \"'{psExecPath}' -d -accepteula -i 1 -w '{workDirPath}' '{myExePath}' '/resume'\"";
            var schCmd = $"schtasks.exe /create /tn \"{RESTART_SHTASK_NAME}\" /ru SYSTEM /sc ONLOGON /tr \"'{restartBatchFilePath}'\"";

            var cmd = new ShellCommand(schCmd);

            if (!cmd.TryExecute(out _))
            {
                //throw new Exception("Unable to schedule after restart resume");
                Log.Error("SystemUtils.ScheduleAfterRestart", "Unable to schedule resume task");
            }
        }