最佳方法在c#中调用外部程序并解析输出

重复

在单独的程序中将控制台输出重定向到文本框 使用C#捕获nslookup shell输出

我希望从我的c#代码中调用外部程序。

我调用的程序,假设foo.exe返回大约12行文本。

我想调用程序并通过输出进行解析。

这样做的最佳方式是什么?

代码片段也赞赏:)

非常感谢你。

using System; using System.Diagnostics; public class RedirectingProcessOutput { public static void Main() { Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.Arguments = "/c dir *.cs"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.Start(); string output = p.StandardOutput.ReadToEnd(); p.WaitForExit(); Console.WriteLine("Output:"); Console.WriteLine(output); } }