当前位置:主页   - 电脑 - 程序设计 - C#
c#中重定向windows控制台程序的输出信息
来源:网络   作者:   更新时间:2012-02-06
收藏此页】    【字号    】    【打印】    【关闭

  这个问题来自论坛提问,答案如下.这只是一个简单的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 + " ";
    }
  }
}

其它资源
来源声明

版权与免责声明
1、本站所发布的文章仅供技术交流参考,本站不主张将其做为决策的依据,浏览者可自愿选择采信与否,本站不对因采信这些信息所产生的任何问题负责。
2、本站部分文章来源于网络,其版权为原权利人所有。由于来源之故,有的文章未能获得作者姓名,署“未知”或“佚名”。对于这些文章,有知悉作者姓名的请告知本站,以便及时署名。如果作者要求删除,我们将予以删除。除此之外本站不再承担其它责任。
3、本站部分文章来源于本站原创,本站拥有所有权利。
4、如对本站发布的信息有异议,请联系我们,经本站确认后,将在三个工作日内做出修改或删除处理。
请参阅权责声明