C#執(zhí)行EXE文件與輸出消息的提取操作
簡介
有時候會需要在c#特別是WPF環(huán)境下調(diào)用其他的程序,這類型的程序以命令行為執(zhí)行環(huán)境,這里就說明下如何調(diào)用exe并傳遞參數(shù)
一般有兩種方法
一種是直接調(diào)用exe程序并執(zhí)行,另一種是調(diào)用cmd.exe然后通過輸入的方式來執(zhí)行指定的程序,前者雖然直接但是有時候不能讀出輸出的信息
因此這里我推薦使用第二個方法,使用異步的方式來創(chuàng)建cmd.exe進(jìn)程,然后調(diào)用我們所需要的程序
1.聲明和初始化一個Process類實例
//進(jìn)程的名稱 string fileName = "cmd.exe"; //測試參數(shù) string para = @"avrdude\avrdude.exe -C avrdude\avrdude.conf -v -p atmega32u4 -c avr109 -P " + portname+" -b 57600 -D -U flash:w:node.hex:i"; //聲明 Process p = new Process();
2.填寫相關(guān)的參數(shù)
這里所需的參數(shù)主要是將輸入和輸出重新定向到Process類的內(nèi)存中,這樣我們就可以通過Process類實例來操作cmd的輸入,同時也可以讀出命令行的輸出
p.StartInfo.CreateNoWindow = true; // 不創(chuàng)建新窗口 p.StartInfo.UseShellExecute = false; //不啟用shell啟動進(jìn)程 p.StartInfo.RedirectStandardInput = true; // 重定向輸入 p.StartInfo.RedirectStandardOutput = true; // 重定向標(biāo)準(zhǔn)輸出 p.StartInfo.RedirectStandardError = true; // 重定向錯誤輸出 p.StartInfo.FileName = fileName;
3.執(zhí)行
p.Start();
4.模擬輸入并結(jié)束輸入
模擬輸入的部分已經(jīng)包括了我們需要調(diào)用的程序以及參數(shù),可以事先用cmd來試試看,這里的
p.StandardInput.WriteLine(para + "&exit"); p.StandardInput.AutoFlush = true; p.StandardInput.Close();
5.獲取輸出
這一部分可以獲取在cmd中執(zhí)行的程序在執(zhí)行完成后所輸出的信息,WaitForExit可以填寫參數(shù)也可以不寫
p.StandardOutput.ReadToEnd(); string output = p.StandardError.ReadToEnd(); // p.OutputDataReceived += new DataReceivedEventHandler(processOutputDataReceived); p.WaitForExit();//參數(shù)單位毫秒,在指定時間內(nèi)沒有執(zhí)行完則強制結(jié)束,不填寫則無限等待 p.Close(); AddInfo(output);
這里我注釋掉的部分代碼是設(shè)置收到輸出之后就跳轉(zhuǎn)的函數(shù),但經(jīng)過測試發(fā)現(xiàn)和我前文所述的第一種方式一樣不可行,很多的程序是用錯誤輸出來輸出信息。因此棄用
這樣就可以完成消息的處理了,具體的結(jié)合WPF的用法(如下圖)我將在后繼進(jìn)行說明
補充:C# 調(diào)用外部程序,并獲取輸出和錯誤信息
1. 同步模式
public void exec(string exePath, string parameters) { System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(); psi.RedirectStandardOutput = true; psi.RedirectStandardError = true; psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; psi.UseShellExecute = false; psi.FileName = exePath; psi.Arguments = parameters; System.Diagnostics.Process process = System.Diagnostics.Process.Start(psi); System.IO.StreamReader outputStreamReader = process.StandardOutput; System.IO.StreamReader errStreamReader = process.StandardError; process.WaitForExit(2000); if (process.HasExited) { string output = outputStreamReader.ReadToEnd(); string error = errStreamReader.ReadToEnd(); MessageBox.Show(output); MessageBox.Show(error); } }
2.異步模式
public void exec(string exePath, string parameters) { Process process = new System.Diagnostics.Process(); process.StartInfo.FileName = exePath; process.StartInfo.Arguments = parameters; process.StartInfo.UseShellExecute = false; process.StartInfo.CreateNoWindow = true; process.StartInfo.RedirectStandardOutput = true; process.Start(); process.BeginOutputReadLine(); process.OutputDataReceived += new DataReceivedEventHandler(processOutputDataReceived); } private void processOutputDataReceived(object sender, DataReceivedEventArgs e) { MessageBox.Show(e.Data); }
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
C#?Socket數(shù)據(jù)接收的三種實現(xiàn)方式
本文主要介紹了C#?Socket數(shù)據(jù)接收的三種實現(xiàn)方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07C#連接Oracle數(shù)據(jù)庫的多種方法總結(jié)
最近小項目當(dāng)中要使用C#來連接Oracle數(shù)據(jù)庫來完成系統(tǒng)的操作,這篇文章主要給大家介紹了關(guān)于C#連接Oracle數(shù)據(jù)庫的多種方法,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04使用位運算實現(xiàn)網(wǎng)頁中的過濾、篩選功能實例
這篇文章主要介紹了使用位運算實現(xiàn)網(wǎng)頁中的過濾、篩選功能實例,一個比常規(guī)拼接SQL字符串更有新意的一個解決思路,需要的朋友可以參考下2014-07-07Unity TextMeshPro實現(xiàn)富文本超鏈接默認(rèn)字體追加字體
這篇文章主要為大家介紹了Unity TextMeshPro實現(xiàn)富文本超鏈接默認(rèn)字體追加字體示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01