I get dinged when my responses are just links to elsewhere. I don't see where the link to the C# Corner article helps much.
The question is 10 years old today but it should have been clarified. The question does not specify whether there are line endings (CrLf) at the end of each question. Assuming there are, as in the following:
string Answer;
Console.Out.WriteLine("First question: ");
Answer = Console.In.ReadLine();
Console.Out.WriteLine("Another question: ");
Answer = Console.In.ReadLine();
Console.Out.WriteLine("Final question: ");
Answer = Console.In.ReadLine();
Then the following can be used to respond to it:
class Program
{
const string FirstQuestion = "First question: ";
const string SecondQuestion = "Another question: ";
const string FinalQuestion = "Final question: ";
static AutoResetEvent Done = new AutoResetEvent(false);
static void Main(string[] args)
{
const string TheProgram = @" ... ";
Process p = new Process();
ProcessStartInfo psi = new ProcessStartInfo(TheProgram);
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
p.StartInfo = psi;
Console.WriteLine("Executing " + TheProgram);
p.Start();
DoPromptsAsync(p);
Done.WaitOne();
}
private static async Task DoPromptsAsync(Process p)
{
StreamWriter sw = p.StandardInput;
StreamReader sr = p.StandardOutput;
string Question;
Question = await sr.ReadLineAsync();
if (Question != FirstQuestion)
return;
sw.WriteLine("First answer");
Console.WriteLine(Question + "answered");
Question = await sr.ReadLineAsync();
if (Question != SecondQuestion)
return;
sw.WriteLine("Second answer");
Console.WriteLine(Question + "answered");
Question = await sr.ReadLineAsync();
if (Question != FinalQuestion)
return;
sw.WriteLine("Final answer");
Console.WriteLine(Question + "answered");
Done.Set();
}
}
The following works in a WPF application; I used a double-click event to test but this could be used in other WPF events.
const string TheProgram = @" ... ";
Process p = new Process();
ProcessStartInfo psi = new ProcessStartInfo(TheProgram);
psi.UseShellExecute = false;
//psi.CreateNoWindow = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
p.StartInfo = psi;
p.Start();
const string FirstQuestion = "First question: ";
const string SecondQuestion = "Another question: ";
const string FinalQuestion = "Final question: ";
StreamWriter sw = p.StandardInput;
StreamReader sr = p.StandardOutput;
string Question;
StringBuilder sb = new StringBuilder("Executing " + TheProgram + "\r\n");
Question = await sr.ReadLineAsync();
if (Question != FirstQuestion)
return;
sw.WriteLine("First answer");
sb.Append(Question + "answered\r\n");
Question = await sr.ReadLineAsync();
if (Question != SecondQuestion)
return;
sw.WriteLine("Second answer");
sb.Append(Question + "answered\r\n");
Question = await sr.ReadLineAsync();
if (Question != FinalQuestion)
return;
sw.WriteLine("Final answer");
sb.Append(Question + "answered\r\n");
ResultBox.Text = sb.ToString();
I think it will be more complicated if there are not line endings after each question.