服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C# 调用exe传参,并获取打印值的实例

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

2022-11-14 12:17小薯仔 C#

这篇文章主要介绍了C# 调用exe传参,并获取打印值的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

调用方法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 返回值中写

?
1
Console.Write(mmma);

补充:c#调用外部exe的方法有简单,有复杂的。

最简单的就是直接利用process类

?
1
2
using System.Diagnostics;
Process.Start(" demo.exe");

想要详细设置的话,就

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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();
}

如果想获取调用程序返回的的结果,那么只需要把上面的稍加修改增加返回值即可:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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;
  }

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

原文链接:https://blog.csdn.net/xiaopanpan92/article/details/77452421

延伸 · 阅读

精彩推荐
  • C#C# 数组中的 indexOf 方法及使用

    C# 数组中的 indexOf 方法及使用

    这篇文章主要介绍了C# 数组中的 indexOf 方法以及indexof方法的使用讲解,需要的朋友可以参考下...

    艹逆风飞翔丶灬沫5172022-02-20
  • C#使用NOPI读取Word、Excel文档内容

    使用NOPI读取Word、Excel文档内容

    这篇文章主要为大家详细介绍了使用NOPI读取Word、Excel文档内容的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    马洪彪3442022-02-21
  • C#C#读写共享文件夹的方法

    C#读写共享文件夹的方法

    这篇文章主要为大家详细介绍了C#读写共享文件夹的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    wybshyy8612022-02-24
  • C#C# ODP.NET 调用Oracle函数返回值时报错的一个解决方案

    C# ODP.NET 调用Oracle函数返回值时报错的一个解决方案

    这篇文章主要介绍了C# ODP.NET 调用Oracle函数返回值时报错的一个解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习...

    邀月6102022-08-10
  • C#C#简单访问SQLite数据库的方法(安装,连接,查询等)

    C#简单访问SQLite数据库的方法(安装,连接,查询等)

    这篇文章主要介绍了C#简单访问SQLite数据库的方法,涉及SQLite数据库的下载、安装及使用C#连接、查询SQLIte数据库的相关技巧,需要的朋友可以参考下...

    郑文亮5082021-11-29
  • C#Unity使用摄像机实现望远镜效果

    Unity使用摄像机实现望远镜效果

    这篇文章主要为大家详细介绍了Unity摄使用像机实现望远镜效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    阳尘子9382022-08-09
  • C#C#实现文件上传与下载功能实例

    C#实现文件上传与下载功能实例

    本篇文章主要介绍了C#实现文件上传与下载,这里整理了详细的代码,有需要的小伙伴可以参考下。...

    pan_junbiao8252021-12-13
  • C#C#实现打造气泡屏幕保护效果

    C#实现打造气泡屏幕保护效果

    本文是介给大家介绍一个很好玩的小程序:气泡屏幕保护!类似于windows的屏保功能,有需要的朋友可以参考一下。...

    李sir4372021-12-08