C# 无法从进程重定向标准输出

C# 无法从进程重定向标准输出,c#,process,C#,Process,我尝试了3-4个版本,但没有一个对我有效。。我有一个小的exe,我不再有它的源代码,但它工作得很好,但我仍然需要它的结果字符串 Process proc = new Process(); ProcessStartInfo StartInfo = new ProcessStartInfo(); StartInfo.RedirectStandardError = true; StartInfo.RedirectStandardOutput = true; StartInfo.FileName = p

我尝试了3-4个版本,但没有一个对我有效。。我有一个小的exe,我不再有它的源代码,但它工作得很好,但我仍然需要它的结果字符串

Process proc = new Process();
ProcessStartInfo StartInfo = new ProcessStartInfo();
StartInfo.RedirectStandardError = true;
StartInfo.RedirectStandardOutput = true;
StartInfo.FileName = path + "calculate.exe";
StartInfo.Arguments = input;
StartInfo.UseShellExecute = false;
StartInfo.CreateNoWindow = true;

proc.StartInfo = StartInfo;

proc.Start();

proc.WaitForExit();

string output = proc.StandardOutput.ReadToEnd();

MessageBox.Show(output);
我收到一个空的留言盒,你知道为什么吗?当我在WaitForExit添加断点时,StandardOutput说它没有重定向,或者进程还没有启动。

您需要设置RedirectStandardOutput,而不是RedirectStandardError。 另外,请确保该过程确实是写入标准输出而不是标准错误

您需要设置RedirectStandardOutput,而不是RedirectStandardError。 另外,请确保该过程实际上是写入标准输出,而不是标准错误。您是否尝试过此操作

// This is the code for the base process
        Process myProcess = new Process();
        // Start a new instance of this program but specify the 'spawned' version.
        ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(args[0], "spawn");
        myProcessStartInfo.UseShellExecute = false;
        myProcessStartInfo.RedirectStandardOutput = true;
        myProcess.StartInfo = myProcessStartInfo;
        myProcess.Start();
        StreamReader myStreamReader = myProcess.StandardOutput;
        // Read the standard output of the spawned process. 
        string myString = myStreamReader.ReadLine();
        Console.WriteLine(myString);

        myProcess.WaitForExit();
        myProcess.Close();
来源:

你试过这个吗

// This is the code for the base process
        Process myProcess = new Process();
        // Start a new instance of this program but specify the 'spawned' version.
        ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(args[0], "spawn");
        myProcessStartInfo.UseShellExecute = false;
        myProcessStartInfo.RedirectStandardOutput = true;
        myProcess.StartInfo = myProcessStartInfo;
        myProcess.Start();
        StreamReader myStreamReader = myProcess.StandardOutput;
        // Read the standard output of the spawned process. 
        string myString = myStreamReader.ReadLine();
        Console.WriteLine(myString);

        myProcess.WaitForExit();
        myProcess.Close();

来源:

该死的自动完成!但是,即使是正确的,我仍然得到空的消息框。@AdnanElezovic你用proc.StandardError.ReadToEnd尝试过吗;也它可能是你的程序输出到stderr。是的!出于某种原因,我不得不重定向两个输出,然后两个都有内容!我只写了应用程序的一部分,所以我不确定它输出到哪个流@格雷,请复制粘贴你的答案,这样我就可以标记它了。@AdnanElezovic SLaks在他的最后一句话中已经写过了。请随意标记他是正确的,因为这就是我的想法。该死的自动完成!但是,即使是正确的,我仍然得到空的消息框。@AdnanElezovic你用proc.StandardError.ReadToEnd尝试过吗;也它可能是你的程序输出到stderr。是的!出于某种原因,我不得不重定向两个输出,然后两个都有内容!我只写了应用程序的一部分,所以我不确定它输出到哪个流@格雷,请复制粘贴你的答案,这样我就可以标记它了。@AdnanElezovic SLaks在他的最后一句话中已经写过了。请随意标记他的观点是正确的,因为这就是我的想法。