Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1006584
  • 博文数量: 150
  • 博客积分: 3017
  • 博客等级: 少校
  • 技术积分: 3829
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-19 14:40
个人简介

Now in Baidu WISE team

文章分类

全部博文(150)

文章存档

2014年(8)

2013年(31)

2012年(111)

分类: C#/.net

2013-11-22 11:21:48

想做一个东西就是命令行执行一个东西,然后可以直接在一个TextBox实时看到结果。
有以下几个要点:
1.启动后用StandardInput.WrinteLine输入命令,就像你平时在CMD窗口里做的一样
         process.StandardInput.WriteLine("java -jar JSONRunner.jar tmp.json");    
2.使用   OutputDataReceived 事件,来处理当cmd得到新的输出时的行为,这里这个行为就是把内容更新到TextBox里
     process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
     
3.别忘了  process.BeginOutputReadLine(); 在开始cmd进程后。

4.因为这个等于是两个县城在操作同一个TextBox,所以会有一个warning. 用下面的语句忽略检查
         Control.CheckForIllegalCrossThreadCalls = false;
当然这个比较生硬。详细的分析和解决方法,请看这里:
http://blog.csdn.net/vodistr03/article/details/6658340

5. 每次刷新TextBox把scroll bar滚动到底部。
    this.textBox1.SelectionStart =this.textBox1.Text.Length;
    this.textBox1.ScrollToCaret();


点击(此处)折叠或打开

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Diagnostics;
  10. using System.IO;

  11. namespace JSONMgr
  12. {
  13.     public partial class OutPutForm : Form
  14.     {
  15.         public Process process = null;
  16.         public OutPutForm()
  17.         {
  18.             InitializeComponent();
  19.         }

  20.   

  21.         private void OutPutForm_Shown(object sender, EventArgs e)
  22.         {
  23.             Control.CheckForIllegalCrossThreadCalls = false;
  24.             process = new Process();
  25.             process.StartInfo.FileName = "cmd.exe";
  26.             process.StartInfo.WorkingDirectory = ".";
  27.             process.StartInfo.UseShellExecute = false;
  28.             process.StartInfo.RedirectStandardInput = true;
  29.             process.StartInfo.RedirectStandardOutput = true;
  30.             process.StartInfo.CreateNoWindow = true;
  31.             //Process.Start("cmd.exe");
  32.             process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
  33.             process.Start();

  34.             process.StandardInput.WriteLine("java -jar JSONRunner.jar tmp.json");
  35.             
  36.             //process.StandardInput.WriteLine("exit");

  37.             process.BeginOutputReadLine();
  38.             //using (StreamWriter sw = new StreamWriter("output.log"))
  39.             //{
  40.             // sw.WriteLine(process.StandardOutput.ReadToEnd());
  41.             //}

  42.         }
  43.         private void OutputHandler(object sendingProcess,DataReceivedEventArgs outLine)
  44.         {
  45.             if (!String.IsNullOrEmpty(outLine.Data))
  46.             {
  47.                 StringBuilder sb = new StringBuilder(this.textBox1.Text);
  48.                 this.textBox1.Text = sb.AppendLine(outLine.Data).ToString();
  49.                 this.textBox1.SelectionStart =this.textBox1.Text.Length;
  50.                 this.textBox1.ScrollToCaret();
  51.             }
  52.         }

  53.         private void OutPutForm_FormClosing(object sender, FormClosingEventArgs e)
  54.         {
  55.             if (process != null)
  56.                 process.Close();
  57.         }
  58.     }
  59. }


点击(此处)折叠或打开

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Diagnostics;
  10. using System.IO;

  11. namespace JSONMgr
  12. {
  13.     public partial class OutPutForm : Form
  14.     {
  15.         public Process process = null;
  16.         public OutPutForm()
  17.         {
  18.             InitializeComponent();
  19.         }

  20.   

  21.         private void OutPutForm_Shown(object sender, EventArgs e)
  22.         {
  23.             Control.CheckForIllegalCrossThreadCalls = false;
  24.             process = new Process();
  25.             process.StartInfo.FileName = "cmd.exe";
  26.             process.StartInfo.WorkingDirectory = ".";
  27.             process.StartInfo.UseShellExecute = false;
  28.             process.StartInfo.RedirectStandardInput = true;
  29.             process.StartInfo.RedirectStandardOutput = true;
  30.             process.StartInfo.CreateNoWindow = true;
  31.             //Process.Start("cmd.exe");
  32.             process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
  33.             process.Start();

  34.             process.StandardInput.WriteLine("java -jar JSONRunner.jar tmp.json");
  35.             
  36.             //process.StandardInput.WriteLine("exit");

  37.             process.BeginOutputReadLine();
  38.             //using (StreamWriter sw = new StreamWriter("output.log"))
  39.             //{
  40.             // sw.WriteLine(process.StandardOutput.ReadToEnd());
  41.             //}

  42.         }
  43.         private void OutputHandler(object sendingProcess,DataReceivedEventArgs outLine)
  44.         {
  45.             if (!String.IsNullOrEmpty(outLine.Data))
  46.             {
  47.                 StringBuilder sb = new StringBuilder(this.textBox1.Text);
  48.                 this.textBox1.Text = sb.AppendLine(outLine.Data).ToString();
  49.                 this.textBox1.SelectionStart =this.textBox1.Text.Length;
  50.                 this.textBox1.ScrollToCaret();
  51.             }
  52.         }

  53.         private void OutPutForm_FormClosing(object sender, FormClosingEventArgs e)
  54.         {
  55.             if (process != null)
  56.                 process.Close();
  57.         }
  58.     }
  59. }


阅读(17623) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

rslonghorn2014-06-16 10:42:38

为什么试用了之后outLine.Data总是返回""呢