c# - RichTextBox c# Windows 窗体中的输出控制台

标签 c#

我正在尝试在 RichTextBox 中进行控制台输出。这是我的代码:

public void Run()
{
    Process myProcess = new Process();
    myProcess.StartInfo.FileName = @"start.bat";
    myProcess.StartInfo.CreateNoWindow = true;
    myProcess.StartInfo.UseShellExecute = false;
    myProcess.StartInfo.RedirectStandardOutput = true;
    myProcess.OutputDataReceived += proc_OutputDataReceived;
    myProcess.Start();
    myProcess.BeginOutputReadLine();
}

public void proc_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
   this.Invoke(new Action(() => 
   richTextBoxConsole.Text += (e.Data + Environment.NewLine)));
}

但是RichTextBox中只显示控制台的第一行。需要实时显示所有线路。哪里错了?

最佳答案

只需将控制台设置为新的 TextBoxStreamWriter。应该可以解决问题。

public class TextBoxStreamWriter : TextWriter
{
    private readonly RichTextBox richTextBox;

    /// <summary>
    /// Initializes a new instance of the <see cref="TextBoxStreamWriter"/> class.
    /// </summary>
    /// <param name="richTextBox">The richTextBox.</param>
    public TextBoxStreamWriter(RichTextBox richTextBox)
    {
        this.richTextBox = richTextBox;
    }

    /// <summary>
    /// Writes a character to the text stream.
    /// </summary>
    /// <param name="value">The character to write to the text stream.</param>
    /// <exception cref="T:System.ObjectDisposedException">The <see cref="T:System.IO.TextWriter"/> is closed. </exception>
    ///   
    /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
    public override void Write(char value)
    {
        if (this.richTextBox.Parent.InvokeRequired)
        {
            Action<char> action = Write;
            this.richTextBox.Parent.Invoke(action, value);
        }
        else
        {
            base.Write(value);
            this.richTextBox.AppendText(value.ToString());
            this.richTextBox.ScrollToCaret();
        }
    }

    /// <summary>
    /// Writes out a formatted string, using the same semantics as 
    /// <see cref="M:System.String.Format(System.String,System.Object)"/>.
    /// </summary>
    /// <param name="format">The formatting string.</param>
    /// <param name="arg0">An object to write into the formatted string.
    /// </param>
    /// <exception cref="T:System.ArgumentNullException">
    ///   <paramref name="format"/> is null. </exception>
    ///   
    /// <exception cref="T:System.ObjectDisposedException">The 
    /// <see cref="T:System.IO.TextWriter"/> is closed. </exception>
    ///   
    /// <exception cref="T:System.IO.IOException">An I/O error occurs. 
    /// </exception>
    ///   
    /// <exception cref="T:System.FormatException">The format specification
    /// in format is invalid.-or- The number indicating an argument to be
    /// formatted is less than zero, or larger than or equal to the number
    /// of provided objects to be formatted. </exception>
    public override void Write(string format, object arg0)
    {
        if (this.richTextBox.Parent.InvokeRequired)
        {
            Action<string, object> action = Write;
            this.richTextBox.Parent.Invoke(action, format, arg0);
        }
        else
        {
            base.Write(format, arg0);
            this.richTextBox.AppendText(string.Format(format, arg0));
            this.richTextBox.ScrollToCaret();
        }
    }

    /// <summary>
    /// When overridden in a derived class, returns the <see cref="T:System.Text.Encoding"/> in which the richTextBox is written.
    /// </summary>
    /// <returns>The Encoding in which the richTextBox is written.</returns>
    public override Encoding Encoding
    {
        get { return Encoding.UTF8; }
    }
}

关于c# - RichTextBox c# Windows 窗体中的输出控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38382819/

相关文章:

c# - 作为属性构造函数输入的属性名称

c# - 无法在单元测试中从 FakeItEasy 模拟中触发事件

c# - 如何创建带有虚线边框或双线边框的文本框

c# - 确定某个区域内是否有任何游戏对象的最佳方法是什么

c# - 从 C++/CLI 函数调用 C# 函数

c# - 如何在 Windows Forms 4 中获取数据 key

c# - 无效的表达式项 "else"| ;预期 (C#)

c# - 如何格式化字符串以在 C# 中包含空格

c# - 在我执行 ctrl+F5 之前,SignalR 在每隔一次页面刷新时中断

c# - 具有多个 dbcontext 的一个事务