0

I'm working on a mechanism, that's capable of creating a Process with a script running on it. At certain points I'm sending input via StandardInput.WriteLine() to that script, which causes it to display information. I want to retrieve only the portion of output, that was displayed between inputs.

for eg.

"some text"

Username: <- there we send the input via StandardInput, and cause script to write some data

"THIS IS THE PORTION OF OUTPUT I WANT TO RETRIEVE"

Password: <- we send another input via StandardInput, I don't want to retrieve this.

"some more text"

I tried using OutputDataReceived event, but it does not fire as expected (I thought it'll fire when I pass input to the script, causing it to spit data).

Can you guys help me come with a solution?

This is the previous try with OutputDataReceived approach.

public ctor()
{
    this.outputStringsList = new List<string>();
    this.process = new Process();
    this.process.StartInfo = this.GetProcessStartInfo();
    this.process.OutputDataReceived += onOutputDataReveived;
    this.process.Start();
    this.process.BeginOutputReadLine();
}

private void onOutputDataReveived(object sender, DataReceivedEventArgs e)
{
    var data = e.Data;
    this.outputStringsList.Add(data);
}

private ProcessStartInfo GetProcessStartInfo()
{
    var start = new ProcessStartInfo();
    start.UseShellExecute = false;
    start.CreateNoWindow = true;
    start.RedirectStandardInput = true;
    start.RedirectStandardOutput = true;

    return start;
}

public void PassArg(string arg)
{
    this.process.StandardInput.WriteLine(arg);
}

It does indeed return output from the process, but it's not on demand

Marcin
  • 121
  • 7
  • [Capturing console output from a .NET application (C#)](https://stackoverflow.com/questions/186822/capturing-console-output-from-a-net-application-c) – Lei Yang Jul 06 '17 at 08:56
  • do you mean you want only part of the test captured? you often need Regex search. – Lei Yang Jul 06 '17 at 09:18
  • @LeiYang Yes, exactly. I want to read only a specific part of the stream. I thought they come dynamically - as soon as something pops in the process, it'll fire the OnDataReceived, but it apparently does not work like that. So basically parsing the ouput after it's all there will be my only choice? – Marcin Jul 06 '17 at 09:22
  • i think so and it's easy and stable if you know simple regex. – Lei Yang Jul 06 '17 at 09:23

0 Answers0