Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在c#中调用外部程序并解析输出的最佳方法 复制品_C#_.net - Fatal编程技术网

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

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

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

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

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

最理想的方法是什么

代码片段(也请欣赏:)


非常感谢。

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);    
    }
}