(以下内容是以参数启动程序的一个应用)
取得当前的系统信息,那么只需要调用 systeminfo 即可
//实例化一个进程类
Process cmd = newProcess();
//获得系统信息,使用的是 systeminfo.exe 这个控制台程序 cmd.StartInfo.FileName = "systeminfo.exe";
//将cmd的标准输入和输出全部重定向到.NET的程序里
cmd.StartInfo.UseShellExecute = false; //此处必须为false否则引发异常
cmd.StartInfo.RedirectStandardInput = true; //标准输入 cmd.StartInfo.RedirectStandardOutput = true; //标准输出
//不显示命令行窗口界面
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
cmd.Start(); //启动进程
//获取输出
//需要说明的:此处是指明开始获取,要获取的内容, //只有等进程退出后才能真正拿到
this.textBox1.Text = cmd.StandardOutput.ReadToEnd();
cmd.WaitForExit();//等待控制台程序执行完成 cmd.Close();//关闭该进程
想做一个东西就是命令行执行一个东西,然后可以直接在一个TextBox实时看到结果。来自 http://blog.chinaunix.net/uid-26456800-id-4005397.html
有以下几个要点:
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();
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Diagnostics;
- using System.IO;
- namespace JSONMgr
- {
- public partial class OutPutForm : Form
- {
- public Process process = null;
- public OutPutForm()
- {
- InitializeComponent();
- }
- private void OutPutForm_Shown(object sender, EventArgs e)
- {
- Control.CheckForIllegalCrossThreadCalls = false;
- process = new Process();
- process.StartInfo.FileName = "cmd.exe";
- process.StartInfo.WorkingDirectory = ".";
- process.StartInfo.UseShellExecute = false;
- process.StartInfo.RedirectStandardInput = true;
- process.StartInfo.RedirectStandardOutput = true;
- process.StartInfo.CreateNoWindow = true;
- //Process.Start("cmd.exe");
- process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
- process.Start();
- process.StandardInput.WriteLine("java -jar JSONRunner.jar tmp.json");
- //process.StandardInput.WriteLine("exit");
- process.BeginOutputReadLine();
- //using (StreamWriter sw = new StreamWriter("output.log"))
- //{
- // sw.WriteLine(process.StandardOutput.ReadToEnd());
- //}
- }
- private void OutputHandler(object sendingProcess,DataReceivedEventArgs outLine)
- {
- if (!String.IsNullOrEmpty(outLine.Data))
- {
- StringBuilder sb = new StringBuilder(this.textBox1.Text);
- this.textBox1.Text = sb.AppendLine(outLine.Data).ToString();
- this.textBox1.SelectionStart =this.textBox1.Text.Length;
- this.textBox1.ScrollToCaret();
- }
- }
- private void OutPutForm_FormClosing(object sender, FormClosingEventArgs e)
- {
- if (process != null)
- process.Close();
- }
- }
- }
浙公网安备 33010602011771号