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