c# - 重定向 cmd.exe 输出/输入时缺少最后一行(在 C# 中)

标签 c# redirect process cmd

我正在尝试通过 C# RichTextArea 启动和控制 CMD.exe(不是通过仅执行一个命令,而是让它等待下一个用户输入,就像在命令提示符中一样)。似乎有效,但它不会重定向最后一行输出(例如经典的“按任意键继续...”执行后或光标前的工作目录),直到我发送另一个输入。这是基本代码:

class CmdPanel : Panel
{
    CmdTextArea textArea;

    Process winCmdProcess;

    public CmdPanel()
    {
        this.BorderStyle = BorderStyle.None;

        textArea = new CmdTextArea(this);
        this.Controls.Add(textArea);

        this.InitializeComponent();
        this.StartShell();
    }

    public void StartShell()
    {
        this.winCmdProcess = new Process();
        this.winCmdProcess.StartInfo.FileName = "cmd.exe";
        this.winCmdProcess.StartInfo.UseShellExecute = false;
        this.winCmdProcess.StartInfo.RedirectStandardOutput = true;
        this.winCmdProcess.StartInfo.RedirectStandardError = true;
        this.winCmdProcess.StartInfo.RedirectStandardInput = true;
        this.winCmdProcess.StartInfo.CreateNoWindow = true;
        this.winCmdProcess.OutputDataReceived += new DataReceivedEventHandler(winCmdProcess_OutputDataReceived);
        this.winCmdProcess.ErrorDataReceived += new DataReceivedEventHandler(winCmdProcess_ErrorDataReceived);

        this.winCmdProcess.Start();
        this.winCmdProcess.BeginOutputReadLine();
        this.winCmdProcess.BeginErrorReadLine();

    }

    /// <summary>
    /// Executes a given command
    /// </summary>
    /// <param name="command"> A string that contains the command, with args</param>
    public void Execute(String command)
    {
        if (!string.IsNullOrWhiteSpace(command))
        {
            this.winCmdProcess.StandardInput.WriteLine(command);
        }
    }

    private void winCmdProcess_OutputDataReceived(object sendingProcess, DataReceivedEventArgs outLine)
    {
        this.ShowOutput(outLine.Data);
    }

    private void winCmdProcess_ErrorDataReceived(object sendingProcess, DataReceivedEventArgs outLine)
    {
        this.ShowOutput(outLine.Data);
    }

    delegate void ShowOutputCallback(string text);
    private void ShowOutput(string text)
    {
        if (this.textArea.InvokeRequired)
        {
            ShowOutputCallback call = new ShowOutputCallback(ShowOutput);
            this.Invoke(call, new object[] { text });
        }
        else
        {
            this.textArea.AppendText(text + Environment.NewLine);
        }
    }

    private void InitializeComponent()
    {

    }

(我没有提供有关文本区域的详细信息,但它将新命令发送到 Execute 方法。)

我错过了什么?

最佳答案

事件won't fire unless a newline is output (或直到流关闭或缓冲区被填满),因此部分行或命令提示符不会触发事件。

关于c# - 重定向 cmd.exe 输出/输入时缺少最后一行(在 C# 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8610615/

相关文章:

c# - Nunit:检查两个对象是否相同

c# - 通过自定义属性生成附加代码

javascript - 有没有办法从后面调用 .jsp 中的函数?

ruby-on-rails - 如何重定向到routes.rb 中的404 页面?

linux - 如何判断 Linux 上是否安装了 Chef 客户端?

c# - 如何取消屏蔽密码文本框并将其屏蔽回密码?

c# - 如何从代码隐藏中获取 javascript 中设置的值?

c - 制作 Linux shell 时的流重定向和管道

mysql - apache进程和Mysql进程过载

android - 本地(同一进程)服务是否需要 bindService?