前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >c#中重定向windows控制台程序的输出信息

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

作者头像
全栈程序员站长
发布2022-07-18 14:21:48
1.2K0
发布2022-07-18 14:21:48
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是全栈君

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

代码语言:javascript
复制
 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 */

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021年12月,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档