C#程序調用外部exe程序

在編寫程序時常常會使用到調用可執行程序的狀況,本文將簡單介紹C#調用exe的方法。在C#中,經過Process類來進行進程操做。 Process類在System.Diagnostics包中。shell

示例一

using System.Diagnostics;
Process p = Process.Start("notepad.exe");
p.WaitForExit();//關鍵,等待外部程序退出後才能往下執行

經過上述代碼能夠調用記事本程序,注意若是不是調用系統程序,則須要輸入全路徑。code

示例二

當須要調用cmd程序時,使用上述調用方法會彈出使人討厭的黑窗。若是要消除,則須要進行更詳細的設置。進程

Process類的StartInfo屬性包含了一些進程啓動信息,其中比較重要的幾個字符串

FileName                可執行程序文件名cmd

Arguments              程序參數,已字符串形式輸入 
CreateNoWindow     是否不須要建立窗口 
UseShellExecute      是否須要系統shell調用程序it

經過上述幾個參數能夠讓討厭的黑屏消失class

System.Diagnostics.Process exep = new System.Diagnostics.Process();
exep.StartInfo.FileName = binStr;
exep.StartInfo.Arguments = cmdStr;
exep.StartInfo.CreateNoWindow = true;
exep.StartInfo.UseShellExecute = false;
exep.Start();exep.WaitForExit();//關鍵,等待外部程序退出後才能往下執行

或者程序

System.Diagnostics.Process exep = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = binStr;
startInfo.Arguments = cmdStr;
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
exep.Start(startInfo);
exep.WaitForExit();//關鍵,等待外部程序退出後才能往下執行
相關文章
相關標籤/搜索