F# 使用F在主应用程序退出时重定向标准输出#

F# 使用F在主应用程序退出时重定向标准输出#,f#,process,redirectstandardoutput,F#,Process,Redirectstandardoutput,假设以下代码: let sw = new StreamWriter("out.txt", false) sw.AutoFlush <- true let proc = new Process() proc.StartInfo.FileName <- path proc.StartInfo.RedirectStandardOutput <- true proc.StartInfo.UseShellExecute <- false proc.OutputDataReceiv

假设以下代码:

let sw = new StreamWriter("out.txt", false)
sw.AutoFlush <- true

let proc = new Process()
proc.StartInfo.FileName <- path
proc.StartInfo.RedirectStandardOutput <- true
proc.StartInfo.UseShellExecute <- false
proc.OutputDataReceived.Add(fun b -> sw.WriteLine b.Data )
proc.Start() |> ignore
proc.BeginOutputReadLine()
let sw=new StreamWriter(“out.txt”,false)

sw.AutoFlush您是否可以通过使用适当的命令行启动进程来执行重定向:?

我认为,如果
重定向标准输出
为false,desco的回答可能会起作用。进程退出后,输出不会写入文件,因为
OutputDataReceived
事件处理程序不再存在。如果可能,我建议将输出文件路径传递给您的程序(假设没有路径意味着写入标准输出)。有了它,你想做的事情应该很容易。

谢谢你的回答。我试过这样做:
proc.StartInfo.Arguments@Oldrich:这可能不起作用,因为
RedirectStandardOutput谢谢,这是一个很好的观点。有时我看不到最简单、最明显的解决方法:)