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;
proc.StandardOutput.ReadToEnd();not give you all the output? Is your problem that you don't know how to split it up?proc.StandardOutput.ReadToEnd()can output all the command output ,but the issue may be that,the next command would be typed according to the previous command output.for example,if the previous command output "How old are you " I will input a number,if the pre command output "What;s your name",I will input myname...AllocConsole. See this answer for example.