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

相关文章推荐

windows 下 Process.Start没有界面(Windows service ui显示的问题)

1.在服务程序安装时编程实现,ProjectInstaller.csusing System;using System.Collections;using System.Collections.Gen...
  • wywyxa
  • wywyxa
  • 2011年08月02日 15:04
  • 771

sap start stop process

  • 2011年03月17日 11:28
  • 884KB
  • 下载

memcached_1.4.14之process_get_command解析

/* ntokens is overwritten here... shrug.. */ static inline void process_get_command(conn *c, token_t...

WPF 利用Process.Start()方法启动指定路径下的exe文件并传递参数

简单来说就是实现一个程序A 打开程序B,并且在打开的时候传递一些参数给B,最后在B窗口上显示出参数,这个小功能也是折腾了我半天。现在把我的过程整理记录下来。 1.首先我们得有一个被调用的程序,新建一...

C#中的Process.Start()方法笔记

1.MSDN链接:http://technet.microsoft.com/zh-cn/library/h6ak8zt5?cs-save-lang=1&cs-lang=csharp#code-snip...

How to start a new process for Android?

We need start a new process when we tap application launcher or start a new service which is in a di...

Process.start五种用法

Process.Start命令行的5种使用方法 1. Process.Start 实例 First, here is an example VB.NET program that uses P...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Start Windows Process / DOS Command
举报原因:
原因补充:

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