0

I have a console application that reads input from console and writes output to console. I need to write another program that should run the first one, mock the console input for it and grab the output. Can you please provide a way of investigation for this problem?

The (very easy) example of console application code:

string input = Console.ReadLine();
int value = int.Parse(input);
Console.WriteLine(value * value);
Sergey Metlov
  • 25,747
  • 28
  • 93
  • 153

1 Answers1

2

You can create the new Process and assign it's StandardInput and StandardOutput properties.

ProcessStartInfo processStartInfo = 
  new ProcessStartInfo(executableName, executableParameter);
processStartInfo.UseShellExecute = false;
processStartInfo.ErrorDialog = false;
processStartInfo.RedirectStandardError = true;
processStartInfo.RedirectStandardInput = true;
processStartInfo.RedirectStandardOutput = true;
Process process = new Process();
process.StartInfo = processStartInfo;
bool processStarted = process.Start();
StreamWriter inputWriter = process.StandardInput;
StreamReader outputReader = process.StandardOutput;
StreamReader errorReader = process.StandardError;
//Write and read process console using inputWriter and outputReader.
process.WaitForExit();
Sasha
  • 8,537
  • 4
  • 49
  • 76