Redirecting Input/Output of cmd.exe

12,390

Does it have to be the same cmd session for all the commands? how about:

    private static void RunCommand(string command)
    {
        var process = new Process()
                          {
                              StartInfo = new ProcessStartInfo("cmd")
                               {
                               UseShellExecute = false,
                               RedirectStandardInput = true,
                               RedirectStandardOutput = true,
                               CreateNoWindow = true,
                               Arguments = String.Format("/c \"{0}\"", command),
                               }
                          };
        process.OutputDataReceived += (s, e) => Console.WriteLine(e.Data);
        process.Start();
        process.BeginOutputReadLine();

        process.WaitForExit();
    }
Share:
12,390
Alex
Author by

Alex

Software Engineer at Philips

Updated on June 29, 2022

Comments

  • Alex
    Alex almost 2 years

    I am having some trouble using the redirected input/output of a process. Originally, I had two applications communicating over tcp/ip. The server tells the client to open up cmd.exe and then gives commands to the client that the client has to redirect to the cmd.exe process. Then the client reads the output and sends it back to the server. Basically I was creating a way to use the command line remotely.

    The problem is it that it works for the first command and then nothing afterwards. I was able to recreate the problem without using tcp/ip.

    Process p = new Process();
    ProcessStartInfo psI = new ProcessStartInfo("cmd");
    psI.UseShellExecute = false;
    psI.RedirectStandardInput = true;
    psI.RedirectStandardOutput = true;
    psI.CreateNoWindow = true;
    p.StartInfo = psI;
    p.Start();
    p.StandardInput.AutoFlush = true;
    p.StandardInput.WriteLine("dir");
    char[] buffer = new char[10000];
    int read = 0;
    // Wait for process to write to output stream
    Thread.Sleep(500);
    while (p.StandardOutput.Peek() > 0)
    {
        read += p.StandardOutput.Read(buffer, read, 10000);
    }
    Console.WriteLine(new string(buffer).Remove(read));
    
    Console.WriteLine("--- Second Output ---");
    p.StandardInput.WriteLine("dir");
    buffer = new char[10000];
    read = 0;
    Thread.Sleep(500);
    while (p.StandardOutput.Peek() > 0)
    {
        read += p.StandardOutput.Read(buffer, read, 10000);
    }
    Console.WriteLine(new string(buffer).Remove(read));
    Console.ReadLine();
    

    This is obviously ugly test code, but I get the same results. I can read the output the first time and then it comes empty the second time around. I am guessing when I use the output stream the first time around I am locking it and preventing cmd.exe to use that stream again? If that is true what is the correct way of using the output stream multiple times after each input command. I would like to do this synchronously to maintain the feeling of the command line. If the only solution is to read the output stream asynchronously is there a way I could figure out generically when the process finished executing my input? I dont want the server to tell the client to execute another command before the first one finishes.

    Thanks.

  • Alex
    Alex over 13 years
    I guess if I am just going to use the console to browse and maybe kick off some programs this would work. I would have to manually keep track of the working directory but this could be a decent temporary solution.