C#編程的時候有很多時候需要啟動外部程序(包括命令行程序或者應(yīng)用程序)。
C# 啟動命令行程序
例如:做上網(wǎng)撥號程序可以啟動命令行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(); //執(zhí)行的結(jié)果內(nèi)容
pro.StandardInput.WriteLine("exit"); //要退出,不然執(zhí)行下一個程序時候會出錯
例如:
用C#調(diào)用CMD.exe,執(zhí)行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();
記得同時要導(dǎo)入:using System.Diagnostics;命名空間。
例如:使用C#調(diào)用外部Ping命令獲取網(wǎng)絡(luò)連接情況
---------------------------------------------------------------
首先,我們用使用Process類,來創(chuàng)建獨立的進程,導(dǎo)入System.Diagnostics,
using System.Diagnostics;
實例一個Process類,啟動一個獨立進程
Process p = new Process();
Process類有一個StartInfo屬性,這個是ProcessStartInfo類,包括了一些屬性和方法,
下面我們用到了他的幾個屬性:
設(shè)定程序名
p.StartInfo.FileName = "cmd.exe";
關(guān)閉Shell的使用
p.StartInfo.UseShellExecute = false;
重定向標準輸入
p.StartInfo.RedirectStandardInput = true;
重定向標準輸出
p.StartInfo.RedirectStandardOutput = true;
重定向錯誤輸出
p.StartInfo.RedirectStandardError = true;
設(shè)置不顯示窗口
p.StartInfo.CreateNoWindow = true;
上面幾個屬性的設(shè)置是比較關(guān)鍵的一步。
既然都設(shè)置好了那就啟動進程吧,
p.Start();
輸入要執(zhí)行的命令,這里就是ping了,
p.StandardInput.WriteLine("ping -n 1 192.192.132.229");
p.StandardInput.WriteLine("exit");
從輸出流獲取命令執(zhí)行結(jié)果,
string strPingTXT = p.StandardOutput.ReadToEnd();
通過分析strPingTXT就知道連接情況了。
C# 啟動應(yīng)用程序的幾種方法:
1. 啟動應(yīng)用程序,不等待其退出。
2. 啟動應(yīng)用程序,等待其退出。
3. 啟動應(yīng)用程序,無限等待其退出。
4. 啟動應(yīng)用程序,通過事件監(jiān)視其退出。
// using System.Diagnostics;
private string appName = "calc.exe";
/// <summary>
/// 1. 啟動外部應(yīng)用程序,不等待其退出
/// </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. 啟動外部應(yīng)用程序,等待其退出
/// </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} 已經(jīng)退出!", this.appName), this.Text,
MessageBoxButtons.OK, MessageBoxIcon.Information);
else
{
// 如果外部應(yīng)用程序沒有結(jié)束運行則強行終止之。
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. 啟動外部應(yīng)用程序,無限等待其退出
/// </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} 已經(jīng)退出!", this.appName), this.Text,
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (ArgumentException ex)
{
MessageBox.Show(ex.Message, this.Text,
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// 4. 啟動外部應(yīng)用程序,通過事件監(jiān)視其退出
/// </summary>
private void button4_Click(object sender, EventArgs e)
{
try
{
// 啟動外部應(yīng)用程序
Process proc = Process.Start(appName);
if (proc != null)
{
// 監(jiān)視進程退出
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("外部應(yīng)用程序程序 {0} 已經(jīng)退出!", this.appName), this.Text,
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
例如:調(diào)用外部程序進行解壓和壓縮。
---------------------------------------------------------------
使用Process對象:
System.Diagnostics.Process p=new System.Diagnostics.Process();
p.StartInfo.FileName="arj.exe" ;//需要啟動的程序名
p.StartInfo.Arguments="-x sourceFile.Arj c:\temp";//啟動參數(shù)
p.Start();//啟動
if(p.HasExisted)//判斷是否運行結(jié)束
p.kill();