I want to call cmd.exe using C#. And I want to write multiple cmd commands in C#.
the most important keys is that: I want to interact with cmd window.the next command is typed according to the last command output.
But now I have tried ,it can input multiple commands but only one output or one outputErr.
I want to achieve one command one output,and the next command is also the same cmd window rather than a new cmd window?
How to solve this problem. the example code is like this
string[] message = new string[2];
ProcessStartInfo info = new ProcessStartInfo();
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
info.RedirectStandardError = true;
info.UseShellExecute = false;
info.FileName = "cmd.exe";
info.CreateNoWindow = true;
Process proc = new Process();
proc.StartInfo = info;
proc.Start();
using (StreamWriter writer = proc.StandardInput)
{
if (writer.BaseStream.CanWrite)
{
foreach (string q in command)
{
writer.WriteLine(q);
}
writer.WriteLine("exit");
}
}
message[0] = proc.StandardError.ReadToEnd();
if (output)
{
message[1] = proc.StandardOutput.ReadToEnd();
}
return message;