C# 启动外部程序的几种方法
2024-09-02 21:45:40
. 启动外部程序,不等待其退出。 . 启动外部程序,等待其退出。 . 启动外部程序,无限等待其退出。 . 启动外部程序,通过事件监视其退出。 // 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);
最新文章
- 如何为Surface Dial设备开发自定义交互功能
- 普林斯顿算法课第四周作业_8Puzzle
- Leetcode: Minimum Unique Word Abbreviation
- fir.im Weekly - 如何打造真正的工程师文化
- kindeditor-在线编辑器
- 在cmd窗口中运行php命令
- linux下使用线程锁互斥访问资源
- IOS从一个APP跳到另一个APP
- Swing(一):JFrame框架窗体
- HTTPS-SSL/TSL与SNI的关系以及同IP多域名虚拟主机的SSL/TSL认证
- Task任务
- Direct2D 简介
- JavaScript编写连连看
- php七牛批量删除空间内的所有文件方法
- python 实现wav的波形显示(时域和频域)
- 你不可错过的Java学习资源清单(包含社区、大牛、专栏、书籍等)
- PowerDesigner 中模型设计导出Excel表格
- softmax与多分类
- 用PHP发送POST请求
- IIS 7.5 上传文件大小限制