c#中重定向windows控制台程序的输出信息

这个问题来自论坛提问,答案如下.这只是一个简单的ipconfig命令.如果是复杂的,比如oracle的exp之类的命令,能在调用的时候显示出来,还是相当酷的.推荐:http://www.cnblog

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

这个问题来自论坛提问,答案如下.这只是一个简单的ipconfig命令.如果是复杂的,比如oracle的exp之类的命令,能在调用的时候显示出来,还是相当酷的.

 using  System;
 using  System.Windows.Forms;
 
 namespace  WindowsApplication8
 {
      public   partial   class  Form1 : Form
      {
          public  Form1()
          {
             InitializeComponent();
         } 
 
          delegate   void  dReadLine( string  strLine);
          private   void  excuteCommand( string  strFile,  string  args, dReadLine onReadLine)
          {
             System.Diagnostics.Process p  =   new  System.Diagnostics.Process();
             p.StartInfo  =   new  System.Diagnostics.ProcessStartInfo();
             p.StartInfo.FileName  =  strFile;
             p.StartInfo.Arguments  =  args;
             p.StartInfo.WindowStyle  =  System.Diagnostics.ProcessWindowStyle.Hidden;
             p.StartInfo.RedirectStandardOutput  =   true ;
             p.StartInfo.UseShellExecute  =   false ;
             p.StartInfo.CreateNoWindow  =   true ;
             p.Start();
             System.IO.StreamReader reader  =  p.StandardOutput; // 截取输出流 
              string  line  =  reader.ReadLine(); // 每次读取一行 
              while  ( ! reader.EndOfStream)
              {
                 onReadLine(line);
                 line  =  reader.ReadLine();
             } 
             p.WaitForExit();
         } 
 
          private   void  button1_Click( object  sender, EventArgs e)
          {
             excuteCommand( " ipconfig " ,  "" ,  new  dReadLine(PrintMessage));
         } 
          private   void  PrintMessage( string  strLine)
          {
              this .textBox1.Text  +=  strLine  +   " " ;
         } 
     } 
 }/* 何问起 hovertree.com */

推荐:http://www.cnblogs.com/roucheng/p/3521864.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/120333.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号