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);
5
  • 3
    Did you tried anything? Show your console application code.. Commented Apr 8, 2013 at 10:38
  • The fact is I really don't know how to begin. Commented Apr 8, 2013 at 10:38
  • Have you looked at all at the Process object? It has plenty of tools to make it easy to read/write from/to the console of a started application. Commented Apr 8, 2013 at 10:38
  • Start looking at this question Commented Apr 8, 2013 at 10:42
  • 1
    This question should put you in the right direction stackoverflow.com/questions/4291912/… Commented Apr 8, 2013 at 10:46

1 Answer 1

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.