【问题标题】:Redirecting cmd Output to textBox将cmd输出重定向到文本框
【发布时间】:2013-05-10 23:24:42
【问题描述】:

我正在将“命令提示符”重新创建到 Windows 窗体中。 应用程序无法正常运行;我不知道为什么。

当表单加载时,它应该运行 cmd.exe(将 cmd 信息加载到“TextBox_Receive”中),但它没有;以及在“textBox_send”(发送输入)中写入任何命令之后;它只会在按“Enter”键 2 或 3 次后显示输入。

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

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

}

【问题讨论】:

    标签: c# redirect cmd output


    【解决方案1】:

    您也可以尝试捕获错误数据。

    为此:

    在你的线路之后

    p.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);
    

    输入这一行

    p.ErrorDataReceived += new DataReceivedEventHandler(SortOutputHandler);
    

    cmd.exe 也可能有问题。

    【讨论】:

    • 谢谢!我忘记了。
    【解决方案2】:

    我认为您的问题是使用OutputDataReceived。在documentation

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

    有关详细信息,请参阅该页面上的示例代码。

    但是 - 我不确定你是否需要走那条路。您是否尝试过直接从StandardOutput 流中读取?

    【讨论】:

    • 谢谢! =) 我重新制作了代码并完美重定向!
    猜你喜欢
    • 2018-01-11
    • 2019-05-05
    • 1970-01-01
    • 2017-04-24
    • 1970-01-01
    • 2016-09-09
    • 2021-09-09
    • 2018-08-29
    • 1970-01-01
    相关资源
    最近更新 更多