C#調用Exe程序示例


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

示例一

using System.Diagnostics;

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

 

 

通過上述代碼可以調用記事本程序,注意如果不是調用系統程序,則需要輸入全路徑。

示例二

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

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

FileName                可執行程序文件名

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

通過上述幾個參數可以讓討厭的黑屏消失

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();//關鍵,等待外部程序退出后才能往下執行

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM