C#调用exe传参获取打印值 C# 调用exe传参,并获取打印值的实例

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C#调用exe传参获取打印值 C# 调用exe传参,并获取打印值的实例

小薯仔   2021-04-16 我要评论
想了解C# 调用exe传参,并获取打印值的实例的相关内容吗,小薯仔在本文为您仔细讲解C#调用exe传参获取打印值的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:C#,调用exe传参,打印值,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;
  }

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们