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的方法有簡單,有復雜的。
最簡單的就是直接利用process類
using System.Diagnostics;
Process.Start(" demo.exe");
想要詳細設置的話,就
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)用結束
process.WaitForExit();
}
如果想獲取調(diào)用程序返回的的結果,那么只需要把上面的稍加修改增加返回值即可:
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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
您可能感興趣的文章:
相關文章
Unity實現(xiàn)繞任意軸任意角度旋轉(zhuǎn)向量
這篇文章主要為大家詳細介紹了Unity實現(xiàn)繞任意軸任意角度旋轉(zhuǎn)向量,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-01-01
c#將list類型轉(zhuǎn)換成DataTable方法示例
將List類型轉(zhuǎn)換成DataTable的通用方法,大家參考使用吧2013-12-12
Winform開發(fā)中使用下拉列表展示字典數(shù)據(jù)的幾種方式
這篇文章介紹了Winform開發(fā)中使用下拉列表展示字典數(shù)據(jù)的幾種方式,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-09-09

