键入cmd命令,将结果显示到richtextbox c#

本文关键字:显示 richtextbox 结果 cmd 命令 键入 | 更新日期: 2023-09-27 17:58:04

在向richtextbox显示cmd命令的结果时遇到问题。到目前为止,我有这个代码:

private void richTextBox2_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            e.Handled = true;
            e.SuppressKeyPress = true;
                ProcessStartInfo cmdStartInfo = new ProcessStartInfo();
                cmdStartInfo.FileName = @"C:'Windows'System32'cmd.exe";
                cmdStartInfo.RedirectStandardOutput = true;
                cmdStartInfo.RedirectStandardError = true;
                cmdStartInfo.RedirectStandardInput = true;
                cmdStartInfo.UseShellExecute = false;
                cmdStartInfo.CreateNoWindow = true;
                Process cmdProcess = new Process();
                cmdProcess.StartInfo = cmdStartInfo;
                cmdProcess.OutputDataReceived += cmd_DataReceived;
                cmdProcess.EnableRaisingEvents = true;
                cmdProcess.Start();
                cmdProcess.BeginOutputReadLine();
                cmdProcess.BeginErrorReadLine();
                cmdProcess.StandardInput.WriteLine(richTextBox2.Text);     
                cmdProcess.StandardInput.WriteLine("exit");                  
                cmdProcess.WaitForExit();
                richTextBox1.Text = richTextBox1.Text + cmd_DataReceived + Environment.NewLine;
        }
    }
static void cmd_DataReceived(object sender, DataReceivedEventArgs e)
    {
        Console.WriteLine("Output from other process");
        Console.WriteLine(e.Data);
    }

我曾尝试将"Console.WriteLine(e.Data);"替换为"richTextBox1.Text=richTextBox1.文本+(e.Data);",但不被接受。我还尝试了"richTextBox1.Text=richTextBox1.Text+cmd_DataReceived";但那对她没有用。然后我试着用messagebox.show(e.data)替换console.writeline,但又一次。。没有命令有效,但不会显示

我知道我复制了大部分代码,它可能是为控制台应用程序准备的。

请帮助

键入cmd命令,将结果显示到richtextbox c#

在等待进程使用cmdProcess.WaitForExit退出时,cmd_DataReceived正在获取所有数据。这意味着您需要存储从回调中获得的数据,在WaitForExit结束后,您可以使用存储的数据内容设置文本框。

一种简单的方法是使用StringBuilder:

    // Have a reference to a stringbuilder that both functions can see
    StringBuilder m_output;
    private void richTextBox2_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            // Before the process starts, create a new stringbuilder
            m_output = new StringBuilder();
            e.Handled = true;
            e.SuppressKeyPress = true;
            ProcessStartInfo cmdStartInfo = new ProcessStartInfo();
            cmdStartInfo.FileName = @"C:'Windows'System32'cmd.exe";
            cmdStartInfo.RedirectStandardOutput = true;
            cmdStartInfo.RedirectStandardError = true;
            cmdStartInfo.RedirectStandardInput = true;
            cmdStartInfo.UseShellExecute = false;
            cmdStartInfo.CreateNoWindow = true;
            Process cmdProcess = new Process();
            cmdProcess.StartInfo = cmdStartInfo;
            cmdProcess.OutputDataReceived += cmd_DataReceived;
            cmdProcess.EnableRaisingEvents = true;
            cmdProcess.Start();
            cmdProcess.BeginOutputReadLine();
            cmdProcess.BeginErrorReadLine();
            cmdProcess.StandardInput.WriteLine(richTextBox2.Text);
            cmdProcess.StandardInput.WriteLine("exit");
            cmdProcess.WaitForExit();
            // And now that everything's done, just set the text
            // to whatever's in the stringbuilder
            richTextBox1.Text = m_output.ToString();
            // We're done with the stringbuilder, let the garbage
            // collector free it
            m_output = null;
        }
    }
    // Note: This is no longer a static method so it has
    // access to the member variables, including m_output
    void cmd_DataReceived(object sender, DataReceivedEventArgs e)
    {
        Debug.WriteLine("Output from other process");
        Debug.WriteLine(e.Data);
        // Add the data, one line at a time, to the string builder
        m_output.AppendLine(e.Data);
    }