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