C#調用外部程序

C#調用外部程序
引用:System.Diagnostics.Process
// 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);
}

總結了一下常用的屬性

System.Diagnostics.Process process = System.Diagnostics.Process.Start(@"c:\\test.txt");

process.EnableRaisingEvents = true; //是否激發關閉事業
//process.Exited +=new EventHandler(process_Exited);
// process.ExitTime //獲取進程退出時間
// process.HasExited //進程是否已終止
// process.Kill //關閉當前進程
// process.MainWindowTitle //進程窗口標題
// process.ProcessName //該進程的名稱
// process.Start //啓動進程
// process.StartTime //進程啓動時間
// process.WaitForExit //無限期的等待進程退出


process.Close();
process.Dispose();

 

如果你要運行一個命令行程序,或者打開一個windows應用程序,或者打開默認的web瀏覽器或email客戶端,..你應該如何在你的C#代碼中實現這個功能呢?
以下這些例子完成相同的任務,你可以使用System.Diagnostics.Process中的類和方法完成這些任務,甚至作的更多。
例1:不管輸出結果,僅僅是運行一個命令行程序:

private void simpleRun_Click(object sender, System.EventArgs e){ System.Diagnostics.Process.Start(@"C:\listfiles.bat");}

 

例2. 得到程序運行結果等待直到程序中止(同步運行程序)

private void runSyncAndGetResults_Click(object sender, System.EventArgs e){ System.Diagnostics.ProcessStartInfo psi =   new System.Diagnostics.ProcessStartInfo(@"C:\listfiles.bat");

psi.RedirectStandardOutput = true; psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; psi.UseShellExecute = false; System.Diagnostics.Process listFiles; listFiles = System.Diagnostics.Process.Start(psi); System.IO.StreamReader myOutput = listFiles.StandardOutput; listFiles.WaitForExit(2000);

if (listFiles.HasExited) 

{  string output = myOutput.ReadToEnd();  this.proce***esults.Text = output; }}

 

例3. 使用用戶機器裏的默認瀏覽器顯示URL

private void launchURL_Click(object sender, System.EventArgs e){ string targetURL = @http://www.baidu.com; System.Diagnostics.Process.Start(targetURL);}

我的看法是,同樣是打開瀏覽器顯示URL,使用例3種的方法比啓動IE並以URL作爲參數要來得合理。例3的代碼將會啓動用戶的默認瀏覽器,而並不總是IE。這樣你更有可能給用戶帶來他們所希望得到的體驗,並且可以利用具有最新連接信息的瀏覽器
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章