C# 調(diào)用exe傳參,并獲取打印值的實例
更新時間:2021年04月16日 11:06:58 作者:小薯仔
這篇文章主要介紹了C# 調(diào)用exe傳參,并獲取打印值的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
調(diào)用方法:
string baseName = System.IO.Directory.GetCurrentDirectory(); // baseName+"/" // string fileName = @"C:\Users\59930\Desktop\20170605\WindowsFormsApp1\WindowsFormsApp1\WindowsFormsApp1\bin\x86\Debug\WindowsFormsApp1.exe"; string fileName = baseName + @"\CardRead.exe"; string para = "1.exe " + code; Process p = new Process(); p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.FileName = fileName; p.StartInfo.CreateNoWindow = true; p.StartInfo.Arguments = para;//參數(shù)以空格分隔,如果某個參數(shù)為空,可以傳入”” p.Start(); p.WaitForExit(); string output = p.StandardOutput.ReadToEnd();
調(diào)用的exe 返回值中寫
Console.Write(mmma);
補充:c#調(diào)用外部exe的方法有簡單,有復(fù)雜的。
最簡單的就是直接利用process類
using System.Diagnostics; Process.Start(" demo.exe");
想要詳細設(shè)置的話,就
public static void RunExeByProcess(string exePath, string argument) { //創(chuàng)建進程 System.Diagnostics.Process process = new System.Diagnostics.Process(); //調(diào)用的exe的名稱 process.StartInfo.FileName = exePath; //傳遞進exe的參數(shù) process.StartInfo.Arguments = argument; process.StartInfo.UseShellExecute = false; //不顯示exe的界面 process.StartInfo.CreateNoWindow = true; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardInput = true; process.Start(); process.StandardInput.AutoFlush = true; //阻塞等待調(diào)用結(jié)束 process.WaitForExit(); }
如果想獲取調(diào)用程序返回的的結(jié)果,那么只需要把上面的稍加修改增加返回值即可:
public static string RunExeByProcess(string exePath, string argument) { //創(chuàng)建進程 System.Diagnostics.Process process = new System.Diagnostics.Process(); //調(diào)用的exe的名稱 process.StartInfo.FileName = exePath; //傳遞進exe的參數(shù) process.StartInfo.Arguments = argument; process.StartInfo.UseShellExecute = false; //不顯示exe的界面 process.StartInfo.CreateNoWindow = true; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardInput = true; process.Start(); process.StandardInput.AutoFlush = true; string result = null; while (!process.StandardOutput.EndOfStream) { result += process.StandardOutput.ReadLine() + Environment.NewLine; } process.WaitForExit(); return result; }
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
您可能感興趣的文章:
相關(guān)文章
c#判斷網(wǎng)絡(luò)連接狀態(tài)的示例分享
這篇文章主要介紹了使用c#判斷網(wǎng)絡(luò)連接狀態(tài)的示例,需要的朋友可以參考下2014-02-02