cmd输出到文本框重复行

cmd output to textbox repeating lines

我正在尝试为我想放入更大项目的功能制作一个测试程序。我正在尝试将批处理文件的输出放入文本框中,以充当用户随后可以通过应用程序与之交互的 inapp 命令提示符输出。用户输入将在稍后出现。到目前为止,我的应用程序正在运行,因此它可以实时读取批处理输出。但是,每次更新时,它都会获取应该在 cmd 中显示的所有内容的精确副本,并将其直接粘贴到先前输出的下方。作为测试,我制作了一个简单的批处理脚本,它使用 -t 参数连续 ping 8.8.8.8。到目前为止,这是我的代码。

using System;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;

namespace ConsoleOutput_test
{
    public partial class Form1 : Form
    {
        private static StringBuilder sortOutput = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            Process sortProcess;
            sortProcess = new Process();
            sortProcess.StartInfo.FileName = "test.bat";
            // Set UseShellExecute to false for redirection.
            sortProcess.StartInfo.CreateNoWindow = true;
            sortProcess.StartInfo.UseShellExecute = false;



            // Redirect the standard output of the sort command.  
            // This stream is read asynchronously using an event handler.
            sortProcess.StartInfo.RedirectStandardOutput = true;
            sortOutput = new StringBuilder("");

            // Set our event handler to asynchronously read the sort output.
            sortProcess.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);

            // Redirect standard input as well.  This stream
            // is used synchronously.
            sortProcess.StartInfo.RedirectStandardInput = true;

            // Start the process.
            sortProcess.Start();



            // Start the asynchronous read of the sort output stream.
            sortProcess.BeginOutputReadLine();
            while (!sortProcess.HasExited)
            {
                Application.DoEvents(); // This keeps your form responsive by processing events
            }
        }

        private void SortOutputHandler(object sendingProcess,
            DataReceivedEventArgs outLine)
        {
            if (txtConsole.InvokeRequired) { txtConsole.BeginInvoke(new DataReceivedEventHandler(SortOutputHandler), new[] { sendingProcess, outLine }); }
            else
            {
            sortOutput.Append(Environment.NewLine + outLine.Data);
            txtConsole.AppendText(sortOutput.ToString());
            }
        }
    }
}

所以现在我们到了我的问题所在。当我 运行 程序时,它会实时显示我想要的内容,但是它会复制并粘贴更新的输出并将其粘贴到上次更新正下方的文本框中。示例如下:

G:\ConsoleOutput test\ConsoleOutput Test\bin\Debug>ping 8.8.8.8 -t

G:\ConsoleOutput test\ConsoleOutput Test\bin\Debug>ping 8.8.8.8 -t

Pinging 8.8.8.8 with 32 bytes of data:

G:\ConsoleOutput test\ConsoleOutput Test\bin\Debug>ping 8.8.8.8 -t

Pinging 8.8.8.8 with 32 bytes of data:
Reply from 8.8.8.8:bytes=32 time=18ms TTL=253


G:\ConsoleOutput test\ConsoleOutput Test\bin\Debug>ping 8.8.8.8 -t

Pinging 8.8.8.8 with 32 bytes of data:
Reply from 8.8.8.8:bytes=32 time=18ms TTL=253
Reply from 8.8.8.8:bytes=32 time=21ms TTL=253

G:\ConsoleOutput test\ConsoleOutput Test\bin\Debug>ping 8.8.8.8 -t

Pinging 8.8.8.8 with 32 bytes of data:
Reply from 8.8.8.8:bytes=32 time=18ms TTL=253
Reply from 8.8.8.8:bytes=32 time=21ms TTL=253
Reply from 8.8.8.8:bytes=32 time=19ms TTL=253

etc

输出就这样一直持续下去。我想完全按照 cmd window.

中显示的输出来显示输出
    G:\ConsoleOutput test\ConsoleOutput Test\bin\Debug>ping 8.8.8.8 -t

Pinging 8.8.8.8 with 32 bytes of data:
Reply from 8.8.8.8:bytes=32 time=18ms TTL=253
Reply from 8.8.8.8:bytes=32 time=21ms TTL=253
Reply from 8.8.8.8:bytes=32 time=19ms TTL=253
Reply from 8.8.8.8:bytes=32 time=18ms TTL=253
Reply from 8.8.8.8:bytes=32 time=21ms TTL=253
Reply from 8.8.8.8:bytes=32 time=19ms TTL=253
Reply from 8.8.8.8:bytes=32 time=18ms TTL=253
Reply from 8.8.8.8:bytes=32 time=21ms TTL=253
Reply from 8.8.8.8:bytes=32 time=19ms TTL=253

它完全按照您的代码所说的去做

      sortOutput.Append(Environment.NewLine + outLine.Data);
        txtConsole.AppendText(sortOutput.ToString());

你说将新行添加到 SortOutput,然后将所有排序输出添加到文本框。你显然会得到重复的行

就这样

        txtConsole.AppendText(Environment.NewLine + outLine.Data);