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

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

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

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

    // 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. 【原】cocos2d-x 2.0.4 不支持https协议 CURLE_UNSUPPORTED_PROTOCOL

    我们项目组用的cocos2d-x版本还比较老,各种好的功能不能用. 今天就让我遇到一个问题,使用CCHttpClient发送http请求的时候,https协议的不支持,返回失败信息如下 errorco ...

  2. Mysql 的存储过程和存储函数

    优点: v 提高安全性 v 简化mysql查询 v 减轻带宽负担 缺点: v 增加服务器负担 v 对我们开发者来说,难度大一点 PHP中的函数 Function funname(参数){ //函数体 ...

  3. mbr grub的区别

    mbr是磁盘的物理扇区,不是程序名: 因为BIOS很小,功能有限,为了完成加载操作系统的功能,就产生了mbr: bios检测到一个硬盘后,将硬盘的0柱面.0磁头.1扇区的内容经过简单判断后, 至内存中 ...

  4. CAKeyframeAnimation

    之所以叫做关键帧动画是因为,这个类可以实现,某一属性按照一串的数值进行动画,就好像制作动画的时候一帧一帧的制作一样. 一般使用的时候  首先通过 animationWithKeyPath 方法 创建一 ...

  5. UNIX系统文件

    密码文件 密码文件又称用户数据库,一般为/etc/passwd,对应的结构为struct passwd,该文件内容大体如下: 描述 passwd字段 用户名 char* pw_name 加密密码 ch ...

  6. 我用dedecms有感

    ---恢复内容开始--- 最近接了一个私单,简单的学校网站,注意,我一看上去是感觉很快,仿站,对方说这个东西你三天就能搞定啦,我也这么想的 (没经验啊) 接下来,我想都没想就用dedecms去做,之前 ...

  7. Codeforces 364

    A 第一题明显统计,注意0和long long(我WA,RE好几次) /* * Problem: A. Matrix * Author: Shun Yao */ #include <string ...

  8. 开元硬件平台 Arduino

    开放源代码的电路图设计,程序开发接口免费下载,也可依个人需求自己修改. Arduino不仅仅是全球最流行的开源硬件,也是一个优秀的硬件开发平台,更是硬件开发的趋势.Arduino简单的开发方式使得开发 ...

  9. Delphi使用FindClass实现动态建立对像(有点像反射)

    相关资料:http://www.blogjava.net/nokiaguy/archive/2008/05/10/199739.html { http://www.blogjava.net/nokia ...

  10. 如何在不装ORACLE的情况下使用PLSQL

    原来我电脑装了oracle跟plsql,然后使用plsql的.后来因为某些原因,我重装了系统,把装的软件都格调了,需要重新装.当时在装plsql的时候我就想,我一直都是直接用plsql远程连接的服务器 ...