csstome 发表于 2013-1-26 14:19:09

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

这个问题来自论坛提问,答案如下.这只是一个简单的ipconfig命令.如果是复杂的,比如oracle的exp之类的命令,能在调用的时候显示出来,还是相当酷的.
<div style="padding: 4px 5.4pt; width: 95%;">http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusingSystem;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusingSystem.Windows.Forms;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifnamespaceWindowsApplication8
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifpublicpartialclassForm1:Form
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifpublicForm1()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifInitializeComponent();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifdelegatevoiddReadLine(stringstrLine);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifprivatevoidexcuteCommand(stringstrFile,stringargs,dReadLineonReadLine)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifSystem.Diagnostics.Processp=newSystem.Diagnostics.Process();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifp.StartInfo=newSystem.Diagnostics.ProcessStartInfo();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifp.StartInfo.FileName=strFile;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifp.StartInfo.Arguments=args;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifp.StartInfo.WindowStyle=System.Diagnostics.ProcessWindowStyle.Hidden;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifp.StartInfo.RedirectStandardOutput=true;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifp.StartInfo.UseShellExecute=false;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifp.StartInfo.CreateNoWindow=true;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifp.Start();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifSystem.IO.StreamReaderreader=p.StandardOutput;//截取输出流
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifstringline=reader.ReadLine();//每次读取一行
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifwhile(!reader.EndOfStream)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifonReadLine(line);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifline=reader.ReadLine();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifp.WaitForExit();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifprivatevoidbutton1_Click(objectsender,EventArgse)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifexcuteCommand("ipconfig","",newdReadLine(PrintMessage));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifprivatevoidPrintMessage(stringstrLine)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifthis.textBox1.Text+=strLine+" ";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}
页: [1]
查看完整版本: c#中重定向windows控制台程序的输出信息