我试图实时获取一个进程的输出,同时将其保存到一个变量中,我尝试了以下方法查看其他stackoverflow问题C# Show output of Process in real time,但是我在行InvalidOperationException上出现了StreamReader myStreamReader = myProcess.StandardOutput错误,我缺少什么?如何解决?

using System;
using System.IO;
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace CallPython
{
    class Program
    {
        static void Main(string[] args)
        {
            // full path of python interpreter
            string python = @"C:\\Python27\python.exe";

            // python app to call
            string myPythonApp = @"C:\\Dropbox\script.py";

            // dummy parameters to send Python script

            string m = @"\\location\\build1";
            string s = "emmc";
            string a = "3980bdd4";
            string ft = "60000";
            string at = "60000";
            string bt = "120000";


            // Create new process start info
            ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(python);

            // make sure we can read the output from stdout
            myProcessStartInfo.UseShellExecute = false;
            myProcessStartInfo.RedirectStandardOutput = true;
            myProcessStartInfo.RedirectStandardError = true;

            // start python app with  arguments
            //myProcessStartInfo.Arguments = myPythonApp + " " + "-m" + " " + m + " " + "-s" + " " + s + " " + "-a" + " " + a + " " + "-ft" + " " + ft + " " + "-at" + " " + at + " " + "-bt" + " " + bt;
            myProcessStartInfo.Arguments = String.Format("{0} -m {1} -s {2} -a {3} -ft {4} -at {5} -bt {6}", myPythonApp, m,s,a,ft,at,bt);

            Process myProcess = new Process();
            // assign start information to the process
            myProcess.StartInfo = myProcessStartInfo;

            Console.WriteLine("Calling Python script with arguments {0} ,{1},{2},{3},{4},{5}", m, s,a,ft,at,bt);
            // start the process
            myProcess.Start();

            myProcess.BeginErrorReadLine();
            myProcess.BeginOutputReadLine();

            StreamReader myStreamReader = myProcess.StandardOutput;

            string myString = myStreamReader.ReadToEnd();


            //Console.WriteLine(myString);
            //Add code for parsing based on myString

            // wait exit signal from the app we called and then close it.
            myProcess.WaitForExit();
            myProcess.Close();
            Console.ReadLine();
        }

    }

}

最佳答案

Process.StandardOutput在以下情况时抛出InvalidOperationException


  已打开StandardOutput流以进行异步读取
  BeginOutputReadLine的操作。


所以你必须


StandardOutput读取
处理OutputDataReceivedEvent并调用BeginOutputReadLine()


但不是两者兼而有之。



例如,在将每行打印到控制台时收集输出:

StreamReader reader = process.StandardOutput;
StringBuilder builder = new StringBuilder();

string line;
while ((line = reader.ReadLine()) != null)
{
    builder.AppendLine(line);
    Console.WriteLine(line);
}

string allLines = builder.ToString();

关于c# - 实时获取进程输出时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38314584/

10-17 01:12