将命令窗口消息重定向到富文本框 [英] Redirecting command window messages to rich text box

查看:93
本文介绍了将命令窗口消息重定向到富文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I am trying to run a batch script from my C# desktop application.

I want to display all errors and messages from command prompt to be redirected to a rich text box. Now, I am looking to display these messages the same time as it is output - not to display all the messages after the batch script has finished running. This is what I did:




private void button1_Click(object sender, EventArgs e)
{
    // Start the child process.
    Process p = new Process();

    // Redirect the output stream of the child process.
    p.StartInfo.CreateNoWindow = true;
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardInput = true;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.RedirectStandardError = true;
    p.StartInfo.FileName = "batchScript.bat";
    p.StartInfo.Arguments = string.Format("\"{0}\" \"{1}\"", arg1, arg2);

    p.Start();

    string text = p.StandardOutput.ReadToEnd();
    richTextBox1.Text = text;

    p.WaitForExit();
}




But my code only outputs all the messages to the text box when the batch script has finished executing. How do I make it printout the messages instantly?

I rarely uses C# and .Net and do not know about the topic in hand to much depth.

Regards,

Sudhi

推荐答案

这是msdn论坛的链接在哪里我问了同样的问题:



http://social.msdn.microsoft.com/Forums/en-US/5880a108-4169-44a5-81f2-6a745438d486 / redirecting-command-window-messages-to-rich-text-box [ ^ ]



不是真正的100%解决方案,但它仍然解决了很多问题。
Here is the link from msdn forum where I asked the same question:

http://social.msdn.microsoft.com/Forums/en-US/5880a108-4169-44a5-81f2-6a745438d486/redirecting-command-window-messages-to-rich-text-box[^]

Not really a 100% solution, but it still solves a lot of the issues.


这篇关于将命令窗口消息重定向到富文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆