C#如何在富文本框中显示所有外部控制台应用程序消息

问题描述 投票:0回答:1

如何读取从外部控制台应用程序发送的所有消息并在富文本框中显示

我有一个运行外部控制台应用程序的C#Windows窗体项目。我下面的代码在富文本框中显示来自应用程序的消息,但未显示所有信息或错误消息。我已经找到了很多示例,指出可以完成此操作,但是它显示在控制台中,但我找不到找到将其转换为在富文本框中显示的方法。

是否可以转换下面的代码以显示所有消息和错误?

string strParameters;
strParameters = "-p " + CmboDeviceList.Text;
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.FileName = TxtPathToExtApp.Text;
startInfo.Arguments = strParameters;
startInfo.CreateNoWindow = true;
process.StartInfo = startInfo;
process.Start();                        
string output = process.StandardOutput.ReadToEnd();
RtxtExtAppOutput.Text = output;
process.WaitForExit();
c# console-application richtextbox messaging
1个回答
0
投票

应尽可能简单

process.Start(); 
while (!process.StandardOutput.EndOfStream && !process.HasExited)
{
   // note the larger the text gets the less efficient this will be
   RtxtExtAppOutput.AppendText(process.StandardOutput.ReadLine());
}

[[注:这是完全未经测试的

© www.soinside.com 2019 - 2024. All rights reserved.