C#調用Exe程式樣本

來源:互聯網
上載者:User

標籤:

在編寫程式時經常會使用到調用可執行程式的情況,本文將簡單介紹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();//關鍵,等待外部程式退出後才能往下執行

 

C#調用Exe程式樣本

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.