c#中重定向windows控制台程序的输出信息
2011-01-30 16:53
676 查看
这个问题来自论坛提问,答案如下.这只是一个简单的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 + " ";
}
}
}
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 + " ";
}
}
}
相关文章推荐
- c#中重定向windows控制台程序的输出信息 - jinjazz - CSDNBlog
- c#中重定向windows控制台程序的输出信息
- c#中重定向windows控制台程序的输出信息
- c#中重定向windows控制台程序的输出信息
- c#中重定向windows控制台程序的输出信息
- c#中重定向windows控制台程序的输出信息
- c#中重定向windows控制台程序的输出信息
- C# 窗体程序,使用控制台输出调试信息
- 调用控制台应用程序并获得程序的输出信息
- C#控制台程序输出等腰三角形并居中显示实例
- Windows进程内标准输出重定向及其在程序调试上的应用
- .Net ( C# ) 调用控制台程序并取得输出结果
- C# 调用外部程序,并获取输出和错误信息
- VS2010 MFC中实现printf调试功能,即MFC程序利用控制台输出调试信息
- C# 调用带参数的控制台程序 并将命令行输出的内容实时输出至窗体中的显示控件
- 程序运行时将打印信息输出到控制台窗口
- Createprocess控制台程序输出重定向
- 在MFC,Win32程序中向控制台(Console)窗口输出调试信息
- c++ 在win32程序中打开控制台 并重定向输出
- Windows下更改C/C++控制台程序文本输出颜色