C#编程的时候有很多时候需要启动外部程序(包括命令行程序或者应用程序)。
C# 启动命令行程序
例如:做上网拨号程序可以启动命令行Rasdial 程序。
---------------------------------------------------------------
Process pro = new Process(); pro.StartInfo.FileName = "cmd"; pro.StartInfo.Arguments = "/c" + "Rasdial/?"; pro.StartInfo.UseShellExecute = false; pro.StartInfo.RedirectStandardInput = true; pro.StartInfo.RedirectStandardOutput = true; pro.StartInfo.RedirectStandardError = true; pro.StartInfo.CreateNoWindow = true; pro.Start();
//string.rdText = pro.StandardOutput.ReadToEnd(); //执行的结果内容 pro.StandardInput.WriteLine("exit"); //要退出,不然执行下一个程序时候会出错
例如:
用C#调用CMD.exe,执行DOS命令,编码 FLV
---------------------------------------------------------------
Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; p.Start(); string strOutput=null; // p.StandardInput.WriteLine("cd D:\\flv\\mplayer"); // p.StandardInput.WriteLine("cd d:"); p.StandardInput.WriteLine(string.Format("D:\\flv\\mplayer\\mencoder \"c:\\vs.wmv\" -o \"c:\\output.flv\" -of lavf -lavfopts i_certify_that_my_video_stream_does_not_use_b_frames -oac mp3lame -lameopts abr:br=56 -ovc lavc -lavcopts vcodec=flv:vbitrate={0}:mbd=2:mv0:trell:v4mv:cbp:last_pred=3:dia=4:cmp=6:vb_strategy=1 -vf scale=512:-3 -ofps 12 -srate 22050",200)); p.StandardInput.WriteLine("exit"); strOutput = p.StandardOutput.ReadToEnd(); Console.WriteLine(strOutput); p.WaitForExit(); p.Close();
记得同时要导入:using System.Diagnostics;命名空间。
例如:使用C#调用外部Ping命令获取网络连接情况 --------------------------------------------------------------- 首先,我们用使用Process类,来创建独立的进程,导入System.Diagnostics,
using System.Diagnostics;
实例一个Process类,启动一个独立进程
Process p = new Process();
Process类有一个StartInfo属性,这个是ProcessStartInfo类,包括了一些属性和方法,
下面我们用到了他的几个属性:
设定程序名
p.StartInfo.FileName = "cmd.exe";
关闭Shell的使用
p.StartInfo.UseShellExecute = false;
重定向标准输入
p.StartInfo.RedirectStandardInput = true;
重定向标准输出
p.StartInfo.RedirectStandardOutput = true;
重定向错误输出
p.StartInfo.RedirectStandardError = true;
设置不显示窗口
p.StartInfo.CreateNoWindow = true;
上面几个属性的设置是比较关键的一步。
既然都设置好了那就启动进程吧,
p.Start();
输入要执行的命令,这里就是ping了,
p.StandardInput.WriteLine("ping -n 1 192.192.132.229");
p.StandardInput.WriteLine("exit");
从输出流获取命令执行结果,
string strPingTXT = p.StandardOutput.ReadToEnd();
通过分析strPingTXT就知道连接情况了。
C# 启动应用程序的几种方法:
1. 启动应用程序,不等待其退出。 2. 启动应用程序,等待其退出。 3. 启动应用程序,无限等待其退出。 4. 启动应用程序,通过事件监视其退出。
// using System.Diagnostics; private string appName = "calc.exe";
/// <summary> /// 1. 启动外部应用程序,不等待其退出 /// </summary> private void button1_Click(object sender, EventArgs e) { Process.Start(appName); MessageBox.Show(String.Format("外部程序 {0} 启动完成!", this.appName), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); }
/// <summary> /// 2. 启动外部应用程序,等待其退出 /// </summary> private void button2_Click(object sender, EventArgs e) { try { Process proc = Process.Start(appName); if (proc != null) { proc.WaitForExit(3000); if (proc.HasExited) MessageBox.Show(String.Format("外部程序 {0} 已经退出!", this.appName), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); else { // 如果外部应用程序没有结束运行则强行终止之。 proc.Kill(); MessageBox.Show(String.Format("外部程序 {0} 被强行终止!", this.appName), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } } catch (ArgumentException ex) { MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// 3. 启动外部应用程序,无限等待其退出 /// </summary> private void button3_Click(object sender, EventArgs e) { try { Process proc = Process.Start(appName); if (proc != null) { proc.WaitForExit(); MessageBox.Show(String.Format("外部程序 {0} 已经退出!", this.appName), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (ArgumentException ex) { MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// 4. 启动外部应用程序,通过事件监视其退出 /// </summary> private void button4_Click(object sender, EventArgs e) { try { // 启动外部应用程序 Process proc = Process.Start(appName); if (proc != null) { // 监视进程退出 proc.EnableRaisingEvents = true; // 指定退出事件方法 proc.Exited += new EventHandler(proc_Exited); } } catch (ArgumentException ex) { MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); } } /// <summary> /// 外部程序退出事件 /// </summary> void proc_Exited(object sender, EventArgs e) { MessageBox.Show(String.Format("外部应用程序程序 {0} 已经退出!", this.appName), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); }
例如:调用外部程序进行解压和压缩。 --------------------------------------------------------------- 使用Process对象: System.Diagnostics.Process p=new System.Diagnostics.Process(); p.StartInfo.FileName="arj.exe" ;//需要启动的程序名 p.StartInfo.Arguments="-x sourceFile.Arj c:\temp";//启动参数 p.Start();//启动 if(p.HasExisted)//判断是否运行结束 p.kill();
|