在c#中调用外部程序并解析输出的最佳方法 复制品
我希望从我的c#代码中调用一个外部程序 我正在调用的程序,假设foo.exe返回大约12行文本 我想调用程序并通过输出进行解析 最理想的方法是什么 代码片段(也请欣赏:)在c#中调用外部程序并解析输出的最佳方法 复制品,c#,.net,C#,.net,我希望从我的c#代码中调用一个外部程序 我正在调用的程序,假设foo.exe返回大约12行文本 我想调用程序并通过输出进行解析 最理想的方法是什么 代码片段(也请欣赏:) 非常感谢。Dupe:差不多了。您需要在ReadToEnd()之后调用WaitForExit(),以避免阻塞问题。这样做是因为答案对我来说是正确的,ReadToEnd和WaitForExit的交互(和顺序)我不太确定。例如,在较长的程序中,请记住使用(process pProcess=new process()){}块处置进程或
非常感谢。Dupe:差不多了。您需要在ReadToEnd()之后调用WaitForExit(),以避免阻塞问题。这样做是因为答案对我来说是正确的,
ReadToEnd
和WaitForExit
的交互(和顺序)我不太确定。例如,在较长的程序中,请记住使用(process pProcess=new process()){}块处置进程或在中使用它
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);
}
}