C#使用Process類調用外部exe程序
在編寫程序時經常會使用到調用可執(zhí)行程序的情況,本文將簡單介紹C#調用exe的方法。在C#中,通過Process類來進行進程操作。 Process類在System.Diagnostics包中。
示例一
Process p = Process.Start("notepad.exe");
p.WaitForExit();//關鍵,等待外部程序退出后才能往下執(zhí)行
通過上述代碼可以調用記事本程序,注意如果不是調用系統(tǒng)程序,則需要輸入全路徑。
示例二
當需要調用cmd程序時,使用上述調用方法會彈出令人討厭的黑窗。如果要消除,則需要進行更詳細的設置。
Process類的StartInfo屬性包含了一些進程啟動信息,其中比較重要的幾個
FileName 可執(zhí)行程序文件名
Arguments 程序參數,已字符串形式輸入
CreateNoWindow 是否不需要創(chuàng)建窗口
UseShellExecute 是否需要系統(tǒng)shell調用程序
通過上述幾個參數可以讓討厭的黑屏消失
exep.StartInfo.FileName = binStr;
exep.StartInfo.Arguments = cmdStr;
exep.StartInfo.CreateNoWindow = true;
exep.StartInfo.UseShellExecute = false;
exep.Start();
exep.WaitForExit();//關鍵,等待外部程序退出后才能往下執(zhí)行
或者
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();//關鍵,等待外部程序退出后才能往下執(zhí)行

