0

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;
3
  • Does the call to proc.StandardOutput.ReadToEnd(); not give you all the output? Is your problem that you don't know how to split it up? Commented Dec 27, 2015 at 15:13
  • The 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... Commented Dec 27, 2015 at 15:42
  • I must admit, this question confuses me. I do not understand what I want to interact with cmd window.the next command is typed according to the last command output. really means. Also, is this a Windows app and you also want a console window? Why not use AllocConsole. See this answer for example. Commented Dec 28, 2015 at 17:11

1 Answer 1

3

The problem is that cmd is a cli application, so proc.StandardOutput.ReadToEnd() will block your thread, you can't simply put the ReadToEnd() in your loop (to execute multiple command).

In my demo, I start a new thread to handle the output.so that i won't block my command input.

  var proc = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = "cmd.exe",
                UseShellExecute = false,
                RedirectStandardOutput = true,
                RedirectStandardInput = true,
                CreateNoWindow = true
            }
        };
        proc.Start();
        StringBuilder sb = new StringBuilder();
        var outStream = proc.StandardOutput;
        var inStream = proc.StandardInput;
        inStream.WriteLine("mkdir test");
        Task.Run(() =>
        {
            while (true)
            {
                Console.WriteLine(outStream.ReadLine());
            }
        });
        Console.WriteLine("dir");
        inStream.WriteLine("dir");

        Console.WriteLine("mkdir test");
        inStream.WriteLine("mkdir test");

        Console.WriteLine("dir");
        inStream.WriteLine("dir");
        Console.ReadLine();

    }

forgive my poor english,

Sign up to request clarification or add additional context in comments.

1 Comment

Penguin, which genus do you belong to? empire?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.