using System; using System.Text; using System.IO; using System.Diagnostics; class RunResults { public int ExitCode; public Exception RunException; public StringBuilder Output; public StringBuilder Error; } class Program { public static RunResults RunExecutable(string executablePath, string arguments, string workingDirectory) { RunResults runResults = new RunResults { Output = new StringBuilder(), Error = new StringBuilder(), RunException = null }; try { if (File.Exists(executablePath)) { using (Process proc = new Process()) { proc.StartInfo.FileName = executablePath; proc.StartInfo.Arguments = arguments; proc.StartInfo.WorkingDirectory = workingDirectory; proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardOutput = true; proc.StartInfo.RedirectStandardError = true; proc.OutputDataReceived += (o, e) => runResults.Output.Append(e.Data).Append(Environment.NewLine); proc.ErrorDataReceived += (o, e) => runResults.Error.Append(e.Data).Append(Environment.NewLine); proc.Start(); proc.BeginOutputReadLine(); proc.BeginErrorReadLine(); proc.WaitForExit(); runResults.ExitCode = proc.ExitCode; } } else { throw new ArgumentException("Invalid executable path.", "exePath"); } } catch (Exception e) { runResults.RunException = e; } return runResults; } public static void Main() { RunResults runResults = RunExecutable("./ConsoleApplication2.exe", "aaa bbb ccc", "."); if (runResults.RunException != null) Console.WriteLine(runResults.RunException); else { Console.WriteLine("Output"); Console.WriteLine("======"); Console.WriteLine(runResults.Output); Console.WriteLine("Error"); Console.WriteLine("====="); Console.WriteLine(runResults.Error); } } }