Note that there are some explanatory texts on larger screens.

plurals
  1. PORedirecting Input/Output of cmd.exe
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <pre><code>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() &gt; 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() &gt; 0) { read += p.StandardOutput.Read(buffer, read, 10000); } Console.WriteLine(new string(buffer).Remove(read)); Console.ReadLine(); </code></pre> <p>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.</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload