Hi
I'm trying to write a program that interacts with an interactive cli
program, that is a program that does some processing but every now and then
requires some user input. My problem is that when I redirect stdin and
stdout of the program I launch, the program seems to run but I never get any
output from it. But if I don't redirect its stdout, I see how it's just
sitting there waiting for my input. To illustrate the problem and make
testing easier I wrote two very small programs, one simulating my
interactive program:
static void Main(string[] args)
{
Console.Write("Login[]");
Console.ReadLine();
Console.Write("\r\nPassword[]:");
Console.ReadLine();
Console.Write("\r\nDo you wish to continue? y/n");
string answer = Console.ReadLine();
if (answer.Equals("y"))
Console.WriteLine("yippie");
else
Console.WriteLine("aborting");
}
And the one supposed to handle this cli program:
static void main(string[] args)
{
ProcessStartInfo psi = new ProcessStartInfo("c:\\temp\\inputreader.exe");
psi.RedirectStandardOutput=true;
psi.RedirectStandardInput=true;
psi.UseShellExecute=false;
psi.CreateNoWindow=true;
Process proc = Process.Start(psi);
StreamReader reader = proc.StandardOutput;
string line = "";
while ((line = reader.ReadLine())!=null)
{
if (line.Equals("Login[]"))
{
proc.StandardInput.WriteLine("login");
}
if (line.Equals("Password[]:"))
{
proc.StandardInput.WriteLine("password");
}
if (line.Equals("Do you wish to continue? y/n"))
{
proc.StandardInput.WriteLine("y");
}
}
}
If anybody could tell me where I'm going wrong I'd much appreciate it.
Regards
Stephan