cmd输出到文本框重复行

时间:2018-12-03 18:21:46

标签: c# outputstream

我正在尝试为要放入更大项目中的功能编写测试程序。我正在尝试将批处理文件的输出放到文本框中,以充当用户随后可以通过应用程序与之交互的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窗口中的显示方式显示输出。

    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

1 个答案:

答案 0 :(得分:1)

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

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

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

只要做

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