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

查看:809
本文介绍了C#重定向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.

当Loads的形式被设置为运行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的异步读取操作期间启用该事件。要启动异步读取操作,必须
重定向进程的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?

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

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