Ida*_*nis 11 c# process output-redirect redirectstandardoutput
我想将Process的标准输出重定向到一个字符串,以便以后解析.我还希望在进程运行时看到屏幕上的输出,而不仅仅是当它完成运行时.
这甚至可能吗?
nma*_*ier 18
来自MSDN的示例:
// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "Write500Lines.exe";
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
另请参阅OutputDataReceived并BeginOutputReadLine()替代ReadToEnd(),以更好地满足您的"在流程运行时查看输出"要求.
如果您想从 C# 应用程序执行 exe 并从中获取输出,则可以使用以下代码
System.Diagnostics.Process p = new System.Diagnostics.Process();            
p.StartInfo.FileName = "PATH TO YOUR FILE";
p.StartInfo.UseShellExecute = false;
p.StartInfo.Arguments = metalType + " " + graphHeight + " " + graphWidth;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;              
p.EnableRaisingEvents = true;
p.Start();            
svgText = p.StandardOutput.ReadToEnd();
using(StreamReader s = p.StandardError)
{
    string error = s.ReadToEnd();
    p.WaitForExit(20000);
}
不要忘记写 p.EnableRaisingEvents = true;
| 归档时间: | 
 | 
| 查看次数: | 20239 次 | 
| 最近记录: |