将cmd输出重定向到textBox [英] Redirecting cmd Output to textBox

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

问题描述

我要在Windows窗体中重新创建命令提示符". 该应用程序无法正常运行;我不知道为什么.

I'm Re-Creating the "command prompt" into a Windows Form. The application is not working properly; and i can't figure exactly why.

当窗体加载时,可以运行cmd.exe(将cmd信息加载到"TextBox_Receive"中),但是没有;并且在"textBox_send"中写入任何命令(发送输入)之后;仅在按"Enter"键2或3次后才会显示输入.

When the form Loads it is suposed to run the cmd.exe (Load the cmd info into "TextBox_Receive"), but it doesnt; and also after writing any command in the "textBox_send" (that sends input); it will only show input after pressing "Enter" key 2 or 3 times.

知道我在这里缺少什么吗?

Any idea what i am missing here?

public partial class Form1 : Form
{
    // Global Variables:
    private static StringBuilder cmdOutput = null;
    Process p;
    StreamWriter SW;

    public Form1()
    {
        InitializeComponent();
        textBox1.Text = Directory.GetCurrentDirectory();
        // TextBox1 Gets the Current Directory
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        checkBox1.Checked = true;
        // This checkBox activates / deactivates writing commands into the "textBox_Send"

        cmdOutput = new StringBuilder("");
        p = new Process();

        p.StartInfo.FileName = "cmd.exe";
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.CreateNoWindow = true;
        p.StartInfo.RedirectStandardInput = true;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.RedirectStandardError = true;
        p.EnableRaisingEvents = true;

        p.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);

        p.Start();

        SW = p.StandardInput;

        p.BeginOutputReadLine();
        p.BeginErrorReadLine();            
    }

    private static void SortOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
// I dont actually understand this part of the code; as this is a "copy" of a snippet i found somewhere. Although it fixed one of my issues to redirect.
    {
        if (!String.IsNullOrEmpty(outLine.Data))
        {
            cmdOutput.Append(Environment.NewLine + outLine.Data);
        }
    }

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        // Send "Enter Key" - Send Command
        if (e.KeyChar == 13)
        {
            SW.WriteLine(txtbox_send.Text);
            txtbox_receive.Text = cmdOutput.ToString();
            txtbox_send.Clear();
        }
    }

    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        // Enable / Disable Sending Commands
        if (checkBox1.Checked)
            txtbox_send.Enabled = true;
        else
            txtbox_send.Enabled = false;
    }
}

}

推荐答案

我认为您的问题是使用OutputDataReceived.在文档:

I think your issue is the use of OutputDataReceived. In the documentation:

在以下位置上的异步读取操作期间启用了事件 标准输出.要启动异步读取操作,您必须 重定向流程的StandardOutput流,添加事件 处理到OutputDataReceived事件,并调用BeginOutputReadLine. 此后,每次处理时都会发出OutputDataReceived事件信号 在重定向的StandardOutput流中写入一行,直到 进程退出或调用CancelOutputRead.

The event is enabled during asynchronous read operations on StandardOutput. To start asynchronous read operations, you must redirect the StandardOutput stream of a Process, add your event handler to the OutputDataReceived event, and call BeginOutputReadLine. Thereafter, the OutputDataReceived event signals each time the process writes a line to the redirected StandardOutput stream, until the process exits or calls CancelOutputRead.

有关详情,请参见该页上的示例代码.

See the example code on that page for more details.

但是-我不确定您是否需要走那条路线.您是否尝试过直接从 StandardOutput 阅读流?

However - I'm not sure you need to go that route. Have you tried just reading directly from the StandardOutput stream?

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

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