Spawning New Processes and reading Output
I was trying to do this.
1. Spawn cmd.exe and have a dialog with it sending commands to it and getting the output.
I did all the relevant things . All those properties and things , But I cant read the output , or I think the process exits itself. The code I am trying is given below.
2. I was trying to spawn bitsadmin.exe with the argument /list but got this error .
BITSADMIN version 2.0 [ 6.6.2600.2180 ]
BITS administration utility.
(C) Copyright 2000-2004 Microsoft Corp.
Unable to get console input mode - 0x80070006
The handle is invalid.
// This is the the code I am using to try it out.
private void button1_Click(object sender, EventArgs e)
{
Process p = new Process();
ProcessStartInfo pInfo = new ProcessStartInfo();
pInfo.RedirectStandardOutput = true;
pInfo.RedirectStandardError = true;
pInfo.RedirectStandardInput = true;
pInfo.FileName = "bitsadmin.exe";
pInfo.Arguments = "/list";
pInfo.CreateNoWindow = true;
pInfo.UseShellExecute = false;
p.StartInfo = pInfo;
p.EnableRaisingEvents = true;
p.OutputDataReceived += new DataReceivedEventHandler(process1_OutputDataReceived);
p.ErrorDataReceived += new DataReceivedEventHandler(process1_ErrorDataReceived);
p.Exited += new EventHandler(process1_Exited);
p.Start ();
//p.StandardInput.WriteLine("dir");
}
private void process1_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
this.Text = "process1_OutputDataReceived";
textBox1.Text += e.Data + "\n";
}
private void process1_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
this.Text = "process1_ErrorDataReceived";
textBox1.Text += e.Data + "\n";
}
private void process1_Exited(object sender, EventArgs e)
{
this.Text = "process1_Exited";
Process p = sender as Process;
textBox1.Text += p.StandardOutput.ReadToEnd() + "\n";
}
}