I have this little function that saves me some headaches from dealing with the terrible System.Diagnostics.Process API:
let HiddenExec (command: string, arguments: string) =
let startInfo = new System.Diagnostics.ProcessStartInfo(command)
startInfo.Arguments <- arguments
startInfo.UseShellExecute <- false
startInfo.RedirectStandardError <- true
startInfo.RedirectStandardOutput <- true
use proc = System.Diagnostics.Process.Start(startInfo)
proc.WaitForExit()
(proc.ExitCode,proc.StandardOutput.ReadToEnd(),proc.StandardError.ReadToEnd())
This works fine because I get a three-element tuple with the results of exitcode, stdout and stderr.
Now suppose I donβt want to βhideβ the execution. That is, I want to write a hypothetical, simpler function called Exec. Then the solution is to not redirect stdout / stderr, and we are done:
let Exec (command: string, arguments: string) =
let startInfo = new System.Diagnostics.ProcessStartInfo(command)
startInfo.Arguments <- arguments
startInfo.UseShellExecute <- false
let proc = System.Diagnostics.Process.Start(startInfo)
proc.WaitForExit()
proc.ExitCode
However, it would be nice if I could reorganize these two functions in order to reduce them into one, and simply pass the "hidden" bool flag to it:
let NewExec (command: string, arguments: string, hidden: bool) =
, NewExec(_,_,false) stdout, stderr ( exitCode, ). , (startInfo.RedirectStandardError <- true), proc.StandardOutput.ReadToEnd(), StandardOut has not been redirected or the process hasn't started yet.
, , Console.WriteLine(eachOutput), , stderr stdout , . , .
, ? Process?: (