Start Windows Process / DOS Command

1. Start Windows Process / DOS command 

public static Process SystemExecuteBG(String command, bool output = true)
        {
            // By default, only redirect the standard out.
            return SystemExecuteBG(command, false, false, output);
        }

        public static Process SystemExecuteBG(String command,
                              bool redirectInput,
                              bool redirectError,
                              bool redirectOutput = true)
        {
            //Create a start info for the process based on filename and arguments
            ProcessStartInfo startInfo;
            startInfo = new ProcessStartInfo("cmd.exe", "/b /c " + command);
            //Set the working directory for the process
            startInfo.WorkingDirectory = Directory.GetCurrentDirectory();
            startInfo.UseShellExecute = false;
            startInfo.CreateNoWindow = true;
            startInfo.RedirectStandardInput = redirectInput;
            startInfo.RedirectStandardOutput = redirectOutput;
            startInfo.RedirectStandardError = redirectError;

            //Create a new process based on the startinfo
            Process process = new Process();
            process.StartInfo = startInfo;
            process.Start();

            if (redirectOutput)
            {
                Console.Out.Flush();
                Console.Out.WriteLine("[EXECUTING] " + command);
                Console.Out.Flush();
            }

            return process;
        }

        public static String SystemExecute(String command, bool failOnNonZeroExit)
        {
            Process p = SystemExecuteBG(command);
            String stdout = p.StandardOutput.ReadToEnd();

            while (!p.HasExited)
            {
                Thread.Sleep(500);
            }

            if (failOnNonZeroExit && p.ExitCode != 0)
            {
                throw new ApplicationException(String.Format("[ErrorID=000104] '{0}' failed. ", command));
            }

            return stdout;
        }

        public static String SystemExecute(String command)
        {
            return SystemExecute(command, true);
        }

        public static void SystemExecute(IEnumerable<string> commands, bool output = true)
        {
            var processes = commands.Select(x => SystemExecuteBG(x, output)).ToArray();
            while (!processes.All(x => x.HasExited))
            {
                Thread.Sleep(500);
            }

            var failedProcess = processes.FirstOrDefault(x => x.ExitCode != 0);
            if (failedProcess != null)
            {
                throw new ApplicationException(String.Format("[ErrorID=000104] '{0} {1}' failed with exit code {2}. ", failedProcess.StartInfo.FileName, failedProcess.StartInfo.Arguments, failedProcess.ExitCode));
            }
        }

        public static int SystemExecuteWithRetry(string cmd, ref string stdout)
        {
            int exitCode = 0;
            for (int i = 0; i < MAX_EXECUTE_RETRY; i++)
            {
                Process p = SystemExecuteBG(cmd);
                stdout = p.StandardOutput.ReadToEnd();
                while (!p.HasExited)
                {
                    Thread.Sleep(500);
                }
                exitCode = p.ExitCode;
                if (exitCode == 0 || exitCode == 1)
                    break;
                Thread.Sleep(60 * 30);
            }
            return exitCode;
        }

        //Start process with no window
        public static Process StartProcess(string execPath, string args, string workingDirectory)
        {
            ProcessStartInfo psi = new ProcessStartInfo(execPath, args);
            psi.CreateNoWindow = true;
            psi.UseShellExecute = false;
            psi.WorkingDirectory = workingDirectory;
            return Process.Start(psi);
        }

        //Start process with no window
        public static Process StartProcess(string execPath, string workingDirectory)
        {
            ProcessStartInfo psi = new ProcessStartInfo(execPath);
            psi.CreateNoWindow = true;
            psi.UseShellExecute = false;
            psi.WorkingDirectory = workingDirectory;
            return Process.Start(psi);
        }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章