Start Windows Process / DOS Command

原创 2015年03月09日 12:24:33

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);
        }

Process执行dos命令

很多时候是需要在java里运行dos命令的,比如sonar测试,博主做云测试系统时遇到过,现整理出来~~~ import java.io.BufferedReader; import java.io...
  • onezg
  • onezg
  • 2017年02月16日 16:55
  • 263

Apache服务启动失败couldn't start errorlog process, unable to open logs

在某用户环境下,Apache服务启动失败,报错信息如下 couldn't start errorlog process  unable to open logs Apache是我们的产品组件,未...
  • mos2046
  • mos2046
  • 2014年09月03日 21:56
  • 3353

failed to start git process

一句话: 你没有安装git
  • AinUser
  • AinUser
  • 2017年10月26日 14:29
  • 746

开发中碰到的问题:org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.7.0

在用as开发项目的过程中,我碰到了这个错误,在网上找到了如下的解决方法,实测有效 被编译的代码或资源有问题( finished with non-zero exit value 1)...
  • qq_24852599
  • qq_24852599
  • 2016年11月17日 10:18
  • 4621

org.gradle.process.internal.ExecException:

com.android.ide.common.process.ProcessException:org.gradle.process.internal.ExecException: Process '...
  • qq_17311561
  • qq_17311561
  • 2016年01月28日 13:48
  • 1411

Dos command in windows

full dos command in windows 一)MD——建立子目录 1.功能:创建新的子目录2.类型:内部命令3.格式:MD[盘符:][路径名]〈子目录名〉4.使用说明:(1)“盘符”:指...
  • chinayaosir
  • chinayaosir
  • 2008年12月10日 17:47
  • 1140

DOS批处理命令之start命令

DOS批处理命令之start命令
  • dadaxiaoxiaode
  • dadaxiaoxiaode
  • 2016年08月13日 16:30
  • 553

关于DOS界面net start MySQL 启动失败的解决办法

最近在dos界面开启MySQL的时候,发现用命令并不能开启MySQL: 网上答案各种各样,最后发现还是找到一种办法: 在MySQL后面加上版本号:net start mys...
  • Vleyong
  • Vleyong
  • 2016年01月04日 14:16
  • 828

DOS批处理命令之for命令

DOS批处理命令之for命令
  • dadaxiaoxiaode
  • dadaxiaoxiaode
  • 2016年08月13日 16:47
  • 1556

android studio下gradle与Git出错解决方法

Error: Gradle: Execution failed for task ‘:mytask’ > A problem occurred starting process ‘command ‘j...
  • sjtu_chenchen
  • sjtu_chenchen
  • 2015年06月04日 14:23
  • 2402
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Start Windows Process / DOS Command
举报原因:
原因补充:

(最多只允许输入30个字)