I'm using Process.Start
from a .NET command-line application to run another command-line application. I don't want to capture the output of the application, I just want it to go to the console directly.
With the program below the output seems to just disappear into thin air. If I leave CreateNoWindow
at the default false
then I do get the output in a fresh console window, but I want it in the original console window. UseShellExecute <- false
is also needed otherwise CreateNoWindow
is forced to false
.
I could do something much more complicated using RedirectStandardOutput
and RedirectStandardError
and then capturing the output and re-printing it, but this is fiddly in combination with WaitForExit
, particularly as in my real application I want to use the version that has a timeout.
Is there any way I can simply get the standard output and error passed through directly?
The behaviour I am seeing is confusing because the documentation for RedirectStandardOutput
does seem to say clearly:
When a Process writes text to its standard stream, that text is typically displayed on the console.
Here's the demonstration code. When I run it with DummyRunner.exe
I get the output from the first bit of code, and when I run it with DummyRunner.exe DummyRunner.exe
I get nothing. Although the code is in F# there's nothing particularly F# specific about this problem that I know of.
module DummyRunner
open System
open System.Diagnostics
[<EntryPoint>]
let main args =
// do something when called with no arguments, just so we can call
// this with itself as an argument to make a self-contained test
if args.Length = 0 then
for n = 1 to 5 do
printfn "Waiting %d" n
System.Threading.Thread.Sleep(1000)
System.Environment.Exit(0)
let cmd = args.[0]
let cmdArgs = args.[1..]
let startInfo = ProcessStartInfo(cmd, String.Join(" ", cmdArgs))
startInfo.UseShellExecute <- false
startInfo.CreateNoWindow <- true
let p = Process.Start(startInfo)
p.WaitForExit()
p.ExitCode
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…