C#調用EXE


1.問題意義

據說界面程序開發,首選C#(像lebview之類的也很好)
但是,能不能用其他語言開發核心代碼,只用C#做界面?畢竟每種語言都有自己擅長的領域.

2.exe程序

比如有個example.exe,能接受4個參數.用cmd的調用方法是

example.exe "1" "a" "2" "3" 

3.C#調用方法

// 調用exe的函數 using System.Diagnostics; public bool StartProcess(string runFilePath, params string[] args) { string s = ""; foreach (string arg in args) { s = s + arg + " "; } s = s.Trim(); Process process = new Process();//創建進程對象 ProcessStartInfo startInfo = new ProcessStartInfo(runFilePath, s); // 括號里是(程序名,參數) process.StartInfo = startInfo; process.Start(); return true; } private void start_craw(object sender, EventArgs e) { string exe_path = "E:/example.exe"; // 被調exe string[] the_args = { "1","2","3","4"}; // 被調exe接受的參數 StartProcess(exe_path, the_args); } 

4.實戰

 
界面設計

 
代碼

給按鍵添加點擊事件,點擊事件觸發start_craw函數


 
點擊事件與函數關聯

5.StartProcess更多的設置

public bool StartProcess(string runFilePath, params string[] args) { string s = ""; foreach (string arg in args) { s = s + arg + " "; } s = s.Trim(); Process process = new Process();//創建進程對象 ProcessStartInfo startInfo = new ProcessStartInfo(runFilePath, s); // 括號里是(程序名,參數) process.StartInfo = startInfo; //process.StartInfo.UseShellExecute = true; //是否使用操作系統的shell啟動 //startInfo.RedirectStandardInput = true; //接受來自調用程序的輸入 //startInfo.RedirectStandardOutput = true; //由調用程序獲取輸出信息 //startInfo.CreateNoWindow = true; //不顯示調用程序的窗口 process.Start(); return true; } 

6.疑難解答

調用外部exe時,當這個exe運行出錯時,會閃退,無法看清錯誤原因
解決:
直接去調試這個被調用的exe即可.



作者:xigua1234
鏈接:https://www.jianshu.com/p/43aa64992706
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯系作者獲得授權並注明出處。


免責聲明!

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



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