C#使用Process类调用外部exe程序
作者:一恋乘魔 时间:2023-02-12 16:54:20
在编写程序时经常会使用到调用可执行程序的情况,本文将简单介绍C#调用exe的方法。在C#中,通过Process类来进行进程操作。 Process类在System.Diagnostics包中。
示例一
using System.Diagnostics;
Process p = Process.Start("notepad.exe");
p.WaitForExit();//关键,等待外部程序退出后才能往下执行
通过上述代码可以调用记事本程序,注意如果不是调用系统程序,则需要输入全路径。
示例二
当需要调用cmd程序时,使用上述调用方法会弹出令人讨厌的黑窗。如果要消除,则需要进行更详细的设置。
Process类的StartInfo属性包含了一些进程启动信息,其中比较重要的几个
FileName 可执行程序文件名
Arguments 程序参数,已字符串形式输入
CreateNoWindow 是否不需要创建窗口
UseShellExecute 是否需要系统shell调用程序
通过上述几个参数可以让讨厌的黑屏消失
System.Diagnostics.Process exep = new System.Diagnostics.Process();
exep.StartInfo.FileName = binStr;
exep.StartInfo.Arguments = cmdStr;
exep.StartInfo.CreateNoWindow = true;
exep.StartInfo.UseShellExecute = false;
exep.Start();
exep.WaitForExit();//关键,等待外部程序退出后才能往下执行
或者
System.Diagnostics.Process exep = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = binStr;
startInfo.Arguments = cmdStr;
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
exep.Start(startInfo);
exep.WaitForExit();//关键,等待外部程序退出后才能往下执行
标签:C#,Process,exe,外部程序
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
浅谈Maven的build生命周期和常用plugin
2022-11-03 01:47:52
![](https://img.aspxhome.com/file/2023/1/75441_0s.jpg)
Flutter WebView 预加载实现方法(Http Server)
2023-06-25 23:14:35
springboot使用redis实现从配置到实战
2023-05-05 09:49:08
SpringBoot配置actuator的代码
2023-02-04 04:45:06
JavaWeb项目Servlet无法访问问题解决
2022-10-10 03:33:52
基于Java生成图片验证码的方法解析
2022-01-22 06:00:37
![](https://img.aspxhome.com/file/2023/5/125335_0s.png)
Java实现二叉树的深度优先遍历和广度优先遍历算法示例
2021-07-16 12:37:17
![](https://img.aspxhome.com/file/2023/7/84177_0s.jpg)
浅谈SpringMVC请求映射handler源码解读
2022-09-11 14:37:36
![](https://img.aspxhome.com/file/2023/4/132134_0s.png)
linux的shell命令检测某个java程序是否执行
2021-07-27 14:32:20
WebService教程详解(一)
2022-02-26 09:59:50
Android电量优化提高手机续航
2022-06-14 11:39:40
![](https://img.aspxhome.com/file/2023/5/137835_0s.png)
MyBatis绑定错误提示BindingException:Invalid bound statement (not found)的解决方法
2021-10-22 03:58:44
Mybatis实现分表插件
2023-07-23 12:41:54
![](https://img.aspxhome.com/file/2023/2/80292_0s.gif)
Android UI实现SlidingMenu侧滑菜单效果
2021-12-08 16:03:34
![](https://img.aspxhome.com/file/2023/3/138363_0s.jpg)
Java的Struts框架中Action的编写与拦截器的使用方法
2021-11-22 02:58:53
![](https://img.aspxhome.com/file/2023/6/103856_0s.jpg)
一个依赖搞定 Spring Boot 接口防盗刷的流程分析
2023-06-01 16:46:41
![](https://img.aspxhome.com/file/2023/4/93344_0s.png)
详解c# AutoMapper 使用方式
2022-05-25 20:51:38
![](https://img.aspxhome.com/file/2023/1/89761_0s.jpg)
C#设计模式实现之迭代器模式
2023-05-31 19:21:58
![](https://img.aspxhome.com/file/2023/8/82118_0s.png)
使用IDEA创建servlet JavaWeb 应用及使用Tomcat本地部署的实现
2023-09-11 06:04:09
![](https://img.aspxhome.com/file/2023/4/57814_0s.jpg)
Android 钱包支付之输入支付密码的实现步骤
2021-09-23 11:17:09
![](https://img.aspxhome.com/file/2023/7/137457_0s.png)