這篇文章給大家分享的是有關C#如何調用exe傳參并獲取打印值的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
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;//參數以空格分隔,如果某個參數為空,可以傳入”” p.Start(); p.WaitForExit(); string output = p.StandardOutput.ReadToEnd();
Console.Write(mmma);
補充:c#調用外部exe的方法有簡單,有復雜的。
最簡單的就是直接利用process類
using System.Diagnostics;
Process.Start(" demo.exe");想要詳細設置的話,就
public static void RunExeByProcess(string exePath, string argument)
{
//創建進程
System.Diagnostics.Process process = new System.Diagnostics.Process();
//調用的exe的名稱
process.StartInfo.FileName = exePath;
//傳遞進exe的參數
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;
//阻塞等待調用結束
process.WaitForExit();
}如果想獲取調用程序返回的的結果,那么只需要把上面的稍加修改增加返回值即可:
public static string RunExeByProcess(string exePath, string argument)
{
//創建進程
System.Diagnostics.Process process = new System.Diagnostics.Process();
//調用的exe的名稱
process.StartInfo.FileName = exePath;
//傳遞進exe的參數
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;
}感謝各位的閱讀!關于“C#如何調用exe傳參并獲取打印值”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。