A
Andreas Schmid
Hi,
I tried to launch Cmd.exe using System.Diagnostics and
interact with it - issue commands and read their output.
However, I only seem to be able to issue one command
since I have to call Process.StandardInput.Close() before
I can call Process.StandardOutput.ReadToEnd() - and I
cannot use the StandardInput thereafter.
Here is the code:
using System;
using System.Diagnostics;
public static void Main()
{
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.RedirectStandardInput =
true;
cmd.StartInfo.RedirectStandardOutput =
true;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.UseShellExecute = false;
cmd.Start();
/* execute "dir" */
cmd.StandardInput.WriteLine("dir");
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
Console.WriteLine
(cmd.StandardOutput.ReadToEnd());
/* execute "ipconfig" does not work:
input stream already closed */
cmd.StandardInput.WriteLine("ipconfig");
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
Console.WriteLine
(cmd.StandardOutput.ReadToEnd());
cmd.Close();
}
I tried to launch Cmd.exe using System.Diagnostics and
interact with it - issue commands and read their output.
However, I only seem to be able to issue one command
since I have to call Process.StandardInput.Close() before
I can call Process.StandardOutput.ReadToEnd() - and I
cannot use the StandardInput thereafter.
Here is the code:
using System;
using System.Diagnostics;
public static void Main()
{
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.RedirectStandardInput =
true;
cmd.StartInfo.RedirectStandardOutput =
true;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.UseShellExecute = false;
cmd.Start();
/* execute "dir" */
cmd.StandardInput.WriteLine("dir");
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
Console.WriteLine
(cmd.StandardOutput.ReadToEnd());
/* execute "ipconfig" does not work:
input stream already closed */
cmd.StandardInput.WriteLine("ipconfig");
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
Console.WriteLine
(cmd.StandardOutput.ReadToEnd());
cmd.Close();
}