FSI和F#中的外部程序启动和输出重定向

时间:2012-08-14 20:33:32

标签: f#

我有以下代码:

let p = new System.Diagnostics.Process();
printfn "installing %A" "installing"
p.StartInfo.FileName <- "powershell.exe";
p.StartInfo.Arguments <- ("/c notepad.exe")
p.StartInfo.RedirectStandardOutput <- true
p.StartInfo.UseShellExecute <- false
p.Start() 
printfn "result ?"
printfn "result %A" (p.StandardOutput.ReadToEnd())
printfn "done"

奇怪的是,在打印结果之前,它等待我在FSI窗口中按Enter键。

这可能是一件小事,但我怎样才能消除这种行为?

我在MSDN上找不到这个。

1 个答案:

答案 0 :(得分:1)

我在VS中的F#Interactive中没有看到这种行为(突出显示代码并按Alt + Enter),或者在带有“fsi Script.fsx”的shell中运行它(Visual Studio 2013 / .NET 4.5.1 )。

我确实必须在开关中添加“-executionpolicy remotesigned”以避免异常(即使在正常的PS shell中,已经设置了),但如果您没有PowerShell配置文件,则可能不会出现这种情况。你。

我看到的是:

(Notepad opens)
installing "installing"
result ?
(I close notepad)
result ""
done

我正在使用fsi Script.fsx运行以下代码。我在PowerShell窗口和普通的CMD提示符下都尝试过它。

let p = new System.Diagnostics.Process();
printfn "installing %A" "installing"
p.StartInfo.FileName <- "powershell.exe";
p.StartInfo.Arguments <- ("-executionpolicy remotesigned /c notepad.exe")
p.StartInfo.RedirectStandardOutput <- true
p.StartInfo.UseShellExecute <- false
p.Start() 
printfn "result ?"
printfn "result %A" (p.StandardOutput.ReadToEnd())
printfn "done"
相关问题