【发布时间】:2014-01-26 17:46:29
【问题描述】:
我正在使用 .NET 命令行应用程序中的 Process.Start 来运行另一个命令行应用程序。我不想捕获应用程序的输出,我只想让它直接进入控制台。
在下面的程序中,输出似乎消失得无影无踪。如果我将CreateNoWindow 保留为默认的false,那么我会在新的控制台窗口中获得输出,但我希望它在原始控制台窗口中。 UseShellExecute <- false 也是必需的,否则 CreateNoWindow 将被强制为 false。
我可以使用RedirectStandardOutput 和RedirectStandardError 做一些更复杂的事情,然后捕获输出并重新打印它,但这与WaitForExit 结合起来非常巧妙,特别是在我想要使用的真实应用程序中the version that has a timeout.
有什么方法可以让标准输出和错误直接通过?
我看到的行为令人困惑,因为RedirectStandardOutput 的文档似乎确实说得很清楚:
当进程将文本写入其标准流时,该文本通常会显示在控制台上。
这是演示代码。当我使用DummyRunner.exe 运行它时,我会从第一段代码中得到输出,而当我使用DummyRunner.exe DummyRunner.exe 运行它时,我什么也得不到。虽然代码在 F# 中,但据我所知,没有什么特别针对 F# 的问题。
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
【问题讨论】: