重定向CMD.EXE的输入/输出 [英] Redirecting Input/Output of cmd.exe

查看:195
本文介绍了重定向CMD.EXE的输入/输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用重定向的输入/输出过程有一些麻烦。最初,我有两个应用程序通过tcp / ip通信。服务器告诉客户端打开cmd.exe,然后给客户端的命令,客户端必须重定向到cmd.exe进程。然后客户端读取输出并将其发送回服务器。基本上我正在创建一种远程使用命令行的方式。

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.

问题是它适用于第一个命令,然后什么都没有。我可以在不使用tcp / ip的情况下重现问题。

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

这是很明显的测试代码,但我得到相同的结果。我可以读取输出的第一次,然后它是空的第二次。我猜猜当我使用输出流第一次,我锁定它,防止cmd.exe再次使用该流?如果是真的,在每个输入命令之后多次使用输出流的正确方式是什么。我想同步这样做以保持命令行的感觉。如果唯一的解决方案是读取输出流异步是有一种方式,我可以一般地知道,当进程完成执行我的输入?我不希望服务器在第一个命令完成之前告诉客户端执行另一个命令。

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.

谢谢。

推荐答案

它是否必须是相同的cmd会话的所有命令?如何:

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

这篇关于重定向CMD.EXE的输入/输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆