这篇文章主要介绍了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();
调用的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;
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持得得之家。如有错误或未考虑完全的地方,望不吝赐教。
织梦狗教程
本文标题为:C# 调用exe传参,并获取打印值的实例


基础教程推荐
猜你喜欢
- Unity 如何获取鼠标停留位置下的物体 2023-04-10
- C# 解析XML和反序列化的示例 2023-04-14
- 实例详解C#实现http不同方法的请求 2022-12-26
- Unity shader实现高斯模糊效果 2023-01-16
- c# – USING块在网站与Windows窗体中的行为不同 2023-09-20
- C#中 Json 序列化去掉null值的方法 2022-11-18
- C#调用摄像头实现拍照功能的示例代码 2023-03-09
- C#中的Linq to JSON操作详解 2023-06-08
- C#通过标签软件Bartender的ZPL命令打印条码 2023-05-16
- C#获取指定目录下某种格式文件集并备份到指定文件夹 2023-05-30