C#執(zhí)行EXE文件與輸出消息的提取操作
簡介
有時候會需要在c#特別是WPF環(huán)境下調用其他的程序,這類型的程序以命令行為執(zhí)行環(huán)境,這里就說明下如何調用exe并傳遞參數(shù)
一般有兩種方法
一種是直接調用exe程序并執(zhí)行,另一種是調用cmd.exe然后通過輸入的方式來執(zhí)行指定的程序,前者雖然直接但是有時候不能讀出輸出的信息
因此這里我推薦使用第二個方法,使用異步的方式來創(chuàng)建cmd.exe進程,然后調用我們所需要的程序
1.聲明和初始化一個Process類實例
//進程的名稱 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.填寫相關的參數(shù)
這里所需的參數(shù)主要是將輸入和輸出重新定向到Process類的內存中,這樣我們就可以通過Process類實例來操作cmd的輸入,同時也可以讀出命令行的輸出
p.StartInfo.CreateNoWindow = true; // 不創(chuàng)建新窗口 p.StartInfo.UseShellExecute = false; //不啟用shell啟動進程 p.StartInfo.RedirectStandardInput = true; // 重定向輸入 p.StartInfo.RedirectStandardOutput = true; // 重定向標準輸出 p.StartInfo.RedirectStandardError = true; // 重定向錯誤輸出 p.StartInfo.FileName = fileName;
3.執(zhí)行
p.Start();
4.模擬輸入并結束輸入
模擬輸入的部分已經(jīng)包括了我們需要調用的程序以及參數(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ù)單位毫秒,在指定時間內沒有執(zhí)行完則強制結束,不填寫則無限等待 p.Close(); AddInfo(output);
這里我注釋掉的部分代碼是設置收到輸出之后就跳轉的函數(shù),但經(jīng)過測試發(fā)現(xiàn)和我前文所述的第一種方式一樣不可行,很多的程序是用錯誤輸出來輸出信息。因此棄用
這樣就可以完成消息的處理了,具體的結合WPF的用法(如下圖)我將在后繼進行說明

補充:C# 調用外部程序,并獲取輸出和錯誤信息
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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關文章
C#請求http向網(wǎng)頁發(fā)送接收數(shù)據(jù)的方法
這篇文章主要為大家詳細介紹了C#請求http向網(wǎng)頁發(fā)送數(shù)據(jù)、網(wǎng)頁接收的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
C#預處理指令之#line,#pragma warning 詳細解析
#line 指令可能由生成過程中的自動中間步驟使用。例如,如果行從原始的源代碼文件中移除,但是您仍希望編譯器基于文件中的原始行號生成輸出,則可以移除行,然后用 #line 模擬原始行號2014-01-01

