5 using System
.Collections
.Generic
;
6 using System
.Diagnostics
;
10 public static class EntryPoint
12 public static bool RunProcess (string filename
, string arguments
, out int exitCode
, out string stdout
, bool capture_stderr
= false)
14 var sb
= new StringBuilder ();
15 var stdout_done
= new System
.Threading
.ManualResetEvent (false);
16 var stderr_done
= new System
.Threading
.ManualResetEvent (false);
17 using (var p
= new Process ()) {
18 p
.StartInfo
.FileName
= filename
;
19 p
.StartInfo
.Arguments
= arguments
;
20 p
.StartInfo
.UseShellExecute
= false;
21 p
.StartInfo
.RedirectStandardOutput
= true;
22 p
.StartInfo
.RedirectStandardError
= capture_stderr
;
24 p
.OutputDataReceived
+= (sender
, e
) => {
30 sb
.AppendLine (e
.Data
);
34 p
.ErrorDataReceived
+= (sender
, e
) => {
40 sb
.AppendLine (e
.Data
);
45 p
.BeginOutputReadLine ();
47 p
.BeginErrorReadLine ();
49 stdout_done
.WaitOne (TimeSpan
.FromSeconds (1));
51 stderr_done
.WaitOne (TimeSpan
.FromSeconds (1));
52 stdout
= sb
.ToString ();
53 exitCode
= p
.ExitCode
;
58 static string RunRedirectOutput (Action action
)
60 var existingOut
= Console
.Out
;
61 var existingErr
= Console
.Error
;
64 using (StringWriter writer
= new StringWriter ()) {
67 Console
.SetOut (writer
);
68 Console
.SetError (writer
);
71 return writer
.ToString ();
75 Console
.SetOut (existingOut
);
76 Console
.SetError (existingErr
);
80 static void RunProcess ()
82 RunProcess ("/bin/echo", "who am i", out int exitCode
, out string stdout
);
83 Console
.Write (stdout
);
86 public static int Main ()
88 var str
= RunRedirectOutput (RunProcess
);
89 Console
.WriteLine ("'{0}'", str
);
90 return str
== "who am i\n" ? 0 : 1;