如何在.NET中生成进程并捕获其STDOUT?
string retMessage = String.Empty;ProcessStartInfo startInfo = new ProcessStartInfo();Process p = new Process();
startInfo.CreateNoWindow = true;startInfo.RedirectStandardOutput = true;startInfo.RedirectStandardInput = true;
startInfo.UseShellExecute = false;startInfo.Arguments = command;startInfo.FileName = exec;p.StartInfo = startInfo
;p.Start();p.OutputDataReceived += new DataReceivedEventHandler(
delegate(object sender, DataReceivedEventArgs e)
{
using (StreamReader output = p.StandardOutput)
{
retMessage = output.ReadToEnd();
}
});p.WaitForExit();return retMessage;OutputDataReceivedWaitForExit()
编辑:
return p.StandardOutput.ReadToEnd();
侃侃尔雅
倚天杖