. 启动外部程序,不等待其退出。

    . 启动外部程序,等待其退出。

    . 启动外部程序,无限等待其退出。

    . 启动外部程序,通过事件监视其退出。

    // 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();

    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# 启动外部程序的几种方法的更多相关文章

  1. [C#.Net]启动外部程序的几种常用方法汇总

    本文汇总了C#启动外部程序的几种常用方法,非常具有实用价值,主要包括如下几种方法: 1. 启动外部程序,不等待其退出. 2. 启动外部程序,等待其退出. 3. 启动外部程序,无限等待其退出. 4. 启 ...

  2. Linux 添加开机启动项的三种方法

    linux 添加开机启动项的三种方法. (1)编辑文件 /etc/rc.local 输入命令:vim /etc/rc.local 将出现类似如下的文本片段: #!/bin/sh## This scri ...

  3. Win11怎么启动任务管理器?Win11启动任务管理器的四种方法

    Win11怎么启动任务管理器?小编为大家带来了Win11启动任务管理器的四种方法,感兴趣的朋友一起看看吧 任务管理器是Windows系统中一项非常实用的功能.不过在最新版Win11中,右击任务栏启动任 ...

  4. android 之 启动画面的两种方法

    现在,当我们打开任意的一个app时,其中的大部分都会显示一个启动界面,展示本公司的logo和当前的版本,有的则直接把广告放到了上面.启动画面的可以分为两种设置方式:一种是两个Activity实现,和一 ...

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

    . 启动外部程序,不等待其退出. . 启动外部程序,等待其退出. . 启动外部程序,无限等待其退出. . 启动外部程序,通过事件监视其退出. 实现代码如下: // using System.Diagn ...

  6. windows linux 双系统默认启动windows 的几种方法

    装了双系统后,在开机时总会有想让一个系统默认启动的时候,一般安装完Ubuntu和XP双系统后,开机时默认的是启动Ubuntu系统,可是当想让XP作为默认启动的系统时怎么办呢? 在早期的Ubuntu系统 ...

  7. mysql安装启动教程(两种方法)

    mysql安装启动: 方法一(简单版): cmd进入mysql安装的bin目录:mysqld.exe –install net start mysql  服务启动(或者选择计算机->(右键)管理 ...

  8. c#代码启动任务管理器的几种方法

    1.直接启动 ProcessStartInfo info = new ProcessStartInfo(); info.FileName = Path.Combine(Environment.GetE ...

  9. Ubuntu下添加开机启动项的2种方法

    1.方法一,编辑rc.loacl脚本 Ubuntu开机之后会执行/etc/rc.local文件中的脚本,所以我们可以直接在/etc/rc.local中添加启动脚本.当然要添加到语句:exit 0 前面 ...

随机推荐

  1. 认识solr结构,了解核心的文件目录

    下载solr并解压后,发现solr的目录里有很多的东西,此时我们可能会感到很恐慌,不知如何下手,下面让我带你认识它. 1.解压后的solr目录结构如下: 虽然里面有很多的文件,但是我们需要的其实就两个 ...

  2. HDU 5744 Keep On Movin

    Keep On Movin Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  3. SSH超时断开(ClientAliveInterval和ClientAliveCountMax )的使用

    有 2个方法 1 配置服务器 打开 /etc/ssh/sshd_config 找到 ClientAliveInterval 参数,如果没有就自己加一行 数值是秒,比如你设置为300,就是5分钟. Cl ...

  4. 总结:ADO.NET在开发中的部分使用方法和技巧

    如何使用 SqlDataAdapter 来检索多个行 以下代码阐明了如何使用 SqlDataAdapter 对象发出可生成 DataSet 或 DataTable 的命令.它从 SQL Server ...

  5. 【windows核心编程】IO完成端口(IOCP)复制文件小例

    1.演示内容 文件复制 2.提要 复制大文件时,使用FILE_FLAG_NO_BUFFERING标志 同时需要注意: 读写文件的偏移地址为 磁盘扇区 的整数倍 读写文件的字节数为 磁盘扇区 的整数倍 ...

  6. Unity中Instantiate一个prefab时需要注意的问题

    在调用Instantiate()方法使用prefab创建对象时,接收Instantiate()方法返回值的变量类型必须和声明prefab变量的类型一致,否则接收变量的值会为null.   比如说,我在 ...

  7. Android自定义TTF字体

    前言: 在Android Design中一个设计手册.在设计手册中有常用的UI图标,图标大小规范等. 其中,有一个TTF字体,以前感觉没什么用.但是我在学习时,常看到有许多开发者使用Google 提供 ...

  8. [Hive - Tutorial] Creating, Showing, Altering, and Dropping Tables

    Creating, Showing, Altering, and Dropping Tables See Hive Data Definition Language for detailed info ...

  9. JEE , EJB概念深入概括

    说起EJB,不得不提JEE,java EE 英文全称为:java Enterprise Edition企业级应用的软件架构,是一种思想,也是一种规范,方便从事这方面的开发者以及开发厂商进行规范性的开发 ...

  10. ERROR (ClientException)

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABPwAAABCCAIAAABM0F+3AAAgAElEQVR4nO2d+29d13Xn7y+xyMvLty