How to execute multiple commands (cmd) c#?

Asked

Viewed 378 times

-1

Good people, I need an application that captures input and output, so that after the execution of a command the same "prompt" is not closed.

I can currently execute commands and capture output in isolation. Follow the function I did:

public string Cmd(string comand)
    {
        cmd.StartInfo.FileName = "cmd.exe";
        cmd.StartInfo.Arguments = string.Format("/c {0}", comand);
        cmd.StartInfo.UseShellExecute = false;
        cmd.StartInfo.CreateNoWindow = true;
        cmd.StartInfo.RedirectStandardOutput = true;
        cmd.Start();

        string output = cmd.StandardOutput.ReadToEnd();
        if (output == "") { output = "Comando executado"; }
        return output;
    }
  • The idea is to keep the prompt open and receive commands from the "other side" until it gives some signal to close?

  • Simm @Joãomartins

2 answers

0

It’s not perfect, it needs some adjustments. I don’t know if that’s exactly what you need.

    static Process execCommand = new Process();
    static void Cmd2(string comando, string argumentos)
    {
        string saida = "";



        execCommand.StartInfo.FileName = comando;
        execCommand.StartInfo.UseShellExecute = false;
        //  execCommand.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        execCommand.StartInfo.Arguments = argumentos;
        execCommand.StartInfo.RedirectStandardOutput = true;
        try
        {
            execCommand.Start();
            saida = execCommand.StandardOutput.ReadToEnd();
            Console.WriteLine(saida);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }




    }

    static void IniciarCmd2(int numeroDeComandos)
    {
        string[] comandos = new string[numeroDeComandos];
        string[] argumentos = new string[numeroDeComandos];

        for (int i = 0; i < comandos.Length; i++)
        {
            Console.WriteLine("Digite o comando n#" + (i+1));
            comandos[i] = Console.ReadLine();
            Console.WriteLine("Digite o argumento n#" + (i+1));
            argumentos[i] = Console.ReadLine();
        }

        for (int i = 0; i < comandos.Length; i++)
        {
            Cmd2(comandos[i], argumentos[i]);

        }
    }
    static void Main(string[] args)
    {



        Console.WriteLine("Quantos comandos pretende executar?");


        IniciarCmd2(Convert.ToInt32(Console.ReadLine()));



        Console.Read();
    }
  • Then a process with several arguments I even got. My big problem is that I would need to run one argument at a time, take the output and only then execute the next command.

0

You can try to join the commands using '&':

string cmdText = "/C primeiroCommando&segundoCommando";
Process.Start("CMD.exe", cmdText);

If it doesn’t work, I suggest creating a temporary batch file (*.bat) and calling it cmd.exe using Process.Start(). The batch file will execute all commands in one process, allowing you to capture the output.``

Browser other questions tagged

You are not signed in. Login or sign up in order to post.