监控的控制台进程

这个话题已经很多前辈已经提及或者说明过,不过今天我还是来炒下冷饭.

很多人在论坛上问及,在不修改现有项目的前提下如何监控其控制台输出?

这里我们就需要用到ProcessStartInfo中的RedirectStandardOutput以及StandardOutput属性

关于StandardOutput以及RedirectStandardOutput,我不在此罗嗦,有兴趣的可以参考下MSDN:

http://msdn.microsoft.com/zh-cn/library/system.diagnostics.processstartinfo.redirectstandardoutput(v=vs.80).aspx

而且其中微软已经给了Stream.ReadToEnd的方法来读取所有的输出.

以下的的例子中提及的是如何逐行输出以及如何及时的输出.

首先准备一个被监控的控制台进程,以下例子为逐行输出

代码如下:

  1. static void Main(string[] args)  
  2. {  
  3.     while (true)  
  4.     {  
  5.         Console.WriteLine("Testing in :" + DateTime.Now.ToString());  
  6.         System.Threading.Thread.Sleep(50);  
  7.     }  
  8.       
  9. }  

    其监控逐行输出代码如下:

    1. static void Main(string[] args)  
    2. {  
    3.     StreamReader sr;  
    4.     ProcessStartInfo s = new ProcessStartInfo();  
    5.     s.RedirectStandardInput = true;  
    6.     s.RedirectStandardOutput = true;  
    7.     s.UseShellExecute = false;  
    8.     s.FileName = @"d:\OutputRepeat.exe";  
    9.     Process p = Process.Start(s);  
    10.     sr = p.StandardOutput;  
    11.     while (!sr.EndOfStream)  
    12.     {  
    13.         string str = sr.ReadLine();  
    14.         Console.WriteLine(str);  
    15.     }  
    16.     p.WaitForExit();  
    17.     p.Close();  
    18. }  

      而有些人会提及如果被监控程式非逐行输出,而其又要很及时的监控这个输出,该怎么办?

      别急,看以下例子,我们只需要小小的修改一个地方,把Stream.ReadLine改成Stream.Read即可


      准备一个被监控的控制台进程,以下例子为非逐行输出

      代码如下:

      1. static void Main(string[] args)  
      2. {  
      3.     while (true)  
      4.     {  
      5.         Console.Write("Testing in :" + DateTime.Now.ToString());  
      6.         System.Threading.Thread.Sleep(50);  
      7.     }  
      8.       
      9. }  

      其及时监控输出代码如下:

      1. static void Main(string[] args)  
      2. {  
      3.     StreamReader sr;  
      4.     ProcessStartInfo s = new ProcessStartInfo();  
      5.     s.RedirectStandardInput = true;  
      6.     s.RedirectStandardOutput = true;  
      7.     s.UseShellExecute = false;  
      8.     s.FileName = @"E:\CSharp\OutputRepeat\bin\Release\OutputRepeat.exe";  
      9.     Process p = Process.Start(s);  
      10.     sr = p.StandardOutput;  
      11.     while (!sr.EndOfStream)  
      12.     {  
      13.         char[] bs = new char[16];  
      14.         int i = sr.Read(bs, 0, 16);  
      15.         foreach (char o in bs)  
      16.         {  
      17.             Console.Write(o);  
      18.         }  
      19.     }  
      20.     p.WaitForExit();  
      21.     p.Close();  
      22. }  

       

      • 0
        点赞
      • 0
        收藏
        觉得还不错? 一键收藏
      • 0
        评论

      “相关推荐”对你有帮助么?

      • 非常没帮助
      • 没帮助
      • 一般
      • 有帮助
      • 非常有帮助
      提交
      评论
      添加红包

      请填写红包祝福语或标题

      红包个数最小为10个

      红包金额最低5元

      当前余额3.43前往充值 >
      需支付:10.00
      成就一亿技术人!
      领取后你会自动成为博主和红包主的粉丝 规则
      hope_wisdom
      发出的红包
      实付
      使用余额支付
      点击重新获取
      扫码支付
      钱包余额 0

      抵扣说明:

      1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
      2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

      余额充值