zoukankan      html  css  js  c++  java
  • c#中重定向windows控制台程序的输出信息

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

        }

    }
  • 相关阅读:
    AbstractRoutingDataSource动态数据源切换,AOP实现动态数据源切换
    SPEC CPU——简介和使用
    Python——sklearn 中 Logistics Regression 的 coef_ 和 intercept_ 的具体意义
    伯努利分布的最大似然估计(最小化交叉熵、分类问题)
    Python关于%matplotlib inline
    MySQL——告别慢SQL,如何去写一手好SQL
    HiveSQL——row_number() over() 使用
    Python——Python缓存技术
    solr——Lucene打分公式的数学推导
    solr——影响Lucene对文档打分的四种方式
  • 原文地址:https://www.cnblogs.com/cl1024cl/p/6204946.html
Copyright © 2011-2022 走看看