C#启动外部程序的几种常用方法汇总

作者:shichen2014 时间:2022-06-21 15:13:11 

本文汇总了C#启动外部程序的几种常用方法,非常具有实用价值,主要包括如下几种方法:

1. 启动外部程序,不等待其退出。
2. 启动外部程序,等待其退出。
3. 启动外部程序,无限等待其退出。
4. 启动外部程序,通过事件监视其退出。

实现代码如下:


// using System.Diagnostics;
private string appName = "calc.exe";
/// <summary>
/// 1. 启动外部程序,不等待其退出
/// </summary>
private void button1_Click(object sender, EventArgs e)
{
Process.Start(appName);
MessageBox.Show(String.Format("外部程序 {0} 启动完成!", this.appName), this.Text,
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
/// <summary>
/// 2. 启动外部程序,等待其退出
/// </summary>
private void button2_Click(object sender, EventArgs e)
{
try
{
Process proc = Process.Start(appName);
if (proc != null)
{
proc.WaitForExit(3000);
if (proc.HasExited) MessageBox.Show(String.Format("外部程序 {0} 已经退出!", this.appName), this.Text,
MessageBoxButtons.OK, MessageBoxIcon.Information);
else
{
// 如果外部程序没有结束运行则强行终止之。
proc.Kill();
MessageBox.Show(String.Format("外部程序 {0} 被强行终止!", this.appName), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
catch (ArgumentException ex)
{
MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// 3. 启动外部程序,无限等待其退出
/// </summary>
private void button3_Click(object sender, EventArgs e)
{
try
{
Process proc = Process.Start(appName);
if (proc != null)
{
proc.WaitForExit();
MessageBox.Show(String.Format("外部程序 {0} 已经退出!", this.appName), this.Text,
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (ArgumentException ex)
{
MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// 4. 启动外部程序,通过事件监视其退出
/// </summary>
private void button4_Click(object sender, EventArgs e)
{
try
{
//启动外部程序
Process proc = Process.Start(appName);
if (proc != null)
{
//监视进程退出
proc.EnableRaisingEvents = true;
//指定退出事件方法
proc.Exited += new EventHandler(proc_Exited);
}
}
catch (ArgumentException ex)
{
MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
///启动外部程序退出事件
/// </summary>
void proc_Exited(object sender, EventArgs e)
{
MessageBox.Show(String.Format("外部程序 {0} 已经退出!", this.appName), this.Text,
MessageBoxButtons.OK, MessageBoxIcon.Information);

读者可以根据情况选择本文实例中的方法,希望能对大家的C#程序设计有一定的帮助借鉴作用。

标签:C#
0
投稿

猜你喜欢

  • SpringCloud网关Gateway架构解析

    2023-03-17 00:18:42
  • Java判断用户名和密码是否符合要求过程详解

    2023-09-24 08:08:36
  • Spring AOP实现接口请求记录到数据库的示例代码

    2023-08-15 19:14:05
  • spring整合JMS实现同步收发消息(基于ActiveMQ的实现)

    2022-06-09 06:00:36
  • android开发之为activity增加左右手势识别示例

    2021-09-30 12:41:50
  • Java class文件格式之常量池_动力节点Java学院整理

    2023-04-14 07:14:06
  • Java重写与重载之间的区别

    2021-06-30 03:16:13
  • 深入理解Spring中bean的生命周期介绍

    2023-02-08 17:21:37
  • Java 面试题和答案 -(上)

    2023-10-08 08:15:56
  • Idea如何导入一个SpringBoot项目的方法(图文教程)

    2022-08-10 22:40:49
  • C#读写操作app.config中的数据应用介绍

    2021-07-25 21:33:19
  • 浅谈java面向对象中四种权限

    2023-03-09 18:43:46
  • c# 遍历 Dictionary的四种方式

    2023-04-17 13:11:10
  • java写入文件的几种方法分享

    2023-06-26 15:56:55
  • Android RecyclerView使用ListAdapter高效刷新数据的操作方法

    2023-06-24 22:22:09
  • Spring Cache手动清理Redis缓存

    2023-11-29 02:49:52
  • C# 解决datagridview控件显示大量数据拖拉卡顿问题

    2022-03-21 12:52:24
  • 解决java 查看JDK中底层源码的实现方法

    2022-03-13 20:40:14
  • java实现发送邮箱验证码

    2022-07-05 07:40:07
  • java多线程的同步方法实例代码

    2022-02-16 19:30:47
  • asp之家 软件编程 m.aspxhome.com