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

新一篇: C#中的属性类 | 旧一篇: c#中子线程控制进度条的一个简单例子

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

    }

}

发表于 @ 2008年05月07日 23:08:41|评论(loading...)|编辑

新一篇: C#中的属性类 | 旧一篇: c#中子线程控制进度条的一个简单例子

评论

#afd88888 发表于2008-05-08 13:25:26  IP: 222.178.150.*
好好的.多多上传.
#hillin 发表于2008-05-09 04:12:27  IP: 125.76.156.*
好东西,谢谢。
吹毛求疵一下,execute拼错了。
#hiwcn 发表于2008-05-10 12:59:26  IP: 222.132.76.*
如果是传参如何改?

ipconfig /all
#jinjazz 发表于2008-05-10 13:01:50  IP: 124.79.65.*
string args
#hiwcn 发表于2008-05-10 13:02:31  IP: 222.132.76.*
如果是传参如何改?

ipconfig /all

excuteCommand("ipconfig", "/all", new dReadLine(PrintMessage));

#hiwcn 发表于2008-05-10 13:10:26  IP: 222.132.76.*
带参示例(对显示效果加入了换行):

/*
* Created by SharpDevelop.
* User: Administrator
* Date: 2008-5-10
* Time: 12:30
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsApplication8
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();

//
// TODO: Add constructor code after the InitializeComponent() call.
//
}

delegate void dReadLine(string strLine);
private void executeCommand(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.Creat
#hiwcn 发表于2008-05-10 13:21:02  IP: 222.132.76.*
呵呵,评论受字符串长度限制无法全部上传源码,可以到以下地址下载源代码:
http://d.download.csdn.net/source/449131
#tengyunjiawu_org 发表于2008-05-12 11:31:19  IP: 118.26.197.*
写日志,听音乐,看视频,欣赏美女的同时挣广告费,上藏拙!
http://cangzhuo.com
#oldnew 发表于2008-05-12 15:21:36  IP: 218.17.90.*
这个有点简单

怎么实现控制台程序输出信息的刷新呢? 一般控制台程序只能一行一行的输出,输出的内容无法修改,但也有控制台程序可以在DOS窗口里修改之前输出的内容,比如一个进度条或百分数的表示,根据任务完成情况不断刷新
#jinjazz 发表于2008-05-12 20:00:29  IP: 222.68.182.*
oldnew的提问很不错,大部分人都不会深入了解Console

-------------------
static void Main(string[] args)
{
Console.WriteLine("********jinjazz*********");
Console.WriteLine("0%");
Console.WriteLine("********jinjazz*********");

for (int i = 0; i < 100; i++)
{
Console.SetCursorPosition(0, 1);
Console.Write(string.Format("{0}%",i));
System.Threading.Thread.Sleep(100);
}
}
#Palme 发表于2008-05-14 17:52:56  IP: 117.32.248.*
怎么样才能把内部命令结果重定向啊?比如dir?
#jinjazz 发表于2008-05-14 18:37:30  IP: 222.68.182.*
cmd 可以传递参数的,比如 cmd /c dir
在这里的调用方式为
excuteCommand("cmd", "/c dir", new dReadLine(PrintMessage));
#invalidate0011 发表于2008-05-14 19:24:34  IP: 211.30.130.*
如果控制台程序是个循环 a.exe
void main()
{
while(true)
{
printf("a\n");
sleep(1000);
}
}

似乎重定向就拿不到想要的结果(一秒钟得到一个'a'),而是一直等待reader.EndOfStream,
否则必须强行杀掉上面这个循环才行,不知道有什么解决方法?
#jinjazz 发表于2008-05-14 19:54:56  IP: 222.68.182.*
那是因为死循环导致windowsform没有处理消息队列,这样改改就可以了。

private void PrintMessage(string strLine)
{
this.textBox1.Text += strLine + " ";
Application.DoEvents();
}
#invalidate0011 发表于2008-05-15 11:56:14  IP: 202.93.162.*
似乎还是不行,我又调试了一下,
在这行就一直等待了,似乎winform一直等待console输出结束

string line = reader.ReadLine();//每次读取一行
#nksurvive 发表于2008-05-15 13:59:13  IP: 61.129.60.*


excuteCommand 改用线程执行,
用Invoke更新form

private void exec()
{
excuteCommand("ipconfig", "", new dReadLine(PrintMessage));
}

private void button1_Click(object sender, EventArgs e)
{
new Thread(new ThreadStart(exec)).Start();

}

更新的地方
this.Invoke(onReadLine,new String[]{line});


#gcsharp 发表于2008-05-16 17:16:39  IP: 219.138.248.*
求C#朋友
QQ223857666
#C# 发表于2008-05-16 17:54:43  IP: 58.16.41.*
博主这个是同步输出的,改成异步输出才具实用性。
#jinjazz 发表于2008-05-16 18:04:32  IP: 222.68.182.*
这个是异步的,测试~~
#yxisyx 发表于2008-05-17 18:38:08  IP: 222.210.237.*
bucuo
#17file.com 发表于2008-05-18 15:07:12  IP: 218.85.88.*
葵花宝典-你的生活经验指南 高清高速免费电视剧 分享导航
厦门生活网-关注"衣食住行" 厦门购物网 【厦门库】 鹭岛生活
集美大学&集美学村BBS 【新闻库】 第八阅读 健康养生
厦门生活论坛-厦门人的视窗 厦门-社区门户 厦门家园 男女
朋友之道,分享你我欢乐! 香蕉你个扒拉 娱乐八卦 明星写真
发表评论  


登录
Csdn Blog version 3.1a
Copyright © 贾涛