调用方法:
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();
调用的exe 返回值中写
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;
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

评论(0)