Redirecting Input/Output of cmd.exe

老子叫甜甜 提交于 2019-12-12 14:19:49

问题


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.


回答1:


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();
    }


来源:https://stackoverflow.com/questions/4116425/redirecting-input-output-of-cmd-exe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!