C# 启动外部程序的几种方法(转自:http://www.cnblogs.com/Slxj/archive/2012/01/12/2320531.html)
2012-01-12 19:18
716 查看
1. 启动外部程序,不等待其退出。
2. 启动外部程序,等待其退出。
3. 启动外部程序,无限等待其退出。
4. 启动外部程序,通过事件监视其退出。
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); }
相关文章推荐
- js 调用C#.NET后台方法 转载自:http://www.cnblogs.com/lizhao/archive/2010/11/23/1990436.html
- asp.net(C#)调用C++程序并交互操作【转】http://www.cnblogs.com/greatverve/archive/2010/06/07/Csharp-transfer-Cpp.html
- C#写文件常用的五种方法(转自:http://www.cnblogs.com/kiddo/archive/2008/03/17/1110061.html)
- 转:自动更新程序源码下载(C#.Net) http://www.cnblogs.com/jenry/archive/2006/08/15/477302.html
- 启动其他APK的Activity方法 (转至http://www.cnblogs.com/lijunamneg/archive/2013/02/26/2934060.html)
- C# 参考之方法参数关键字:params、ref及out 引用-http://www.cnblogs.com/hunts/archive/2007/01/13/619620.html
- C++中cout输出字符型指针地址值的方法 http://www.cnblogs.com/wxxweb/archive/2011/05/20/2052256.html
- CMake 使用方法 http://www.cnblogs.com/lyq105/archive/2010/12/03/1895067.html
- Debian(Linux)+XAMPP(LAMPP)+Zend Studio + PHP +XDebug 完整的开发环境配置方法。 转摘:http://www.cnblogs.com/kungfupanda/archive/2010/11/25/1887812.html
- XPath语法 在C#中使用XPath示例 【转http://www.cnblogs.com/yukaizhao/archive/2011/07/25/xpath.html】非常详细的文章
- 关于Jquery中ajax方法data参数用法的总结http://www.cnblogs.com/tim190/archive/2010/10/20/1856523.html
- ASP.NET跨页面传值的几种方法 转http://www.cnblogs.com/kivenhou/archive/2010/03/02/1676773.html#1773015
- 在asp.net中读取XML文件信息的4种方法 (http://www.cnblogs.com/liping19851014/archive/2007/10/16/926137.html)
- C#使用命令行方式实现Ping简单功能 http://www.cnblogs.com/kevinton/archive/2007/06/28/798581.html
- A*算法的C#实现 -最短路径--摘自http://www.cnblogs.com/China-Dragon/archive/2009/11/23/1608803.html
- Php魔术方法(http://www.cnblogs.com/xiaochaohuashengmi/archive/2011/09/22/2185034.html)
- C# 调用C++的dll,通过DllImport方式。 from http://www.cnblogs.com/xiaokang088/archive/2011/04/08/2009673.html
- C# 实现的多线程异步Socket数据包接收器框架(来源http://www.cnblogs.com/wcfgroup/archive/2008/10/06/1304512.html)
- IHttpModule.Init方法被执行多次的原因(转自:http://www.cnblogs.com/mingda/archive/2009/02/11/1388709.html)
- 解决:无法连接到 WMI 提供程序。您没有权限或者该服务器无法访问。 (转帖:http://www.cnblogs.com/furenjun/archive/2011/09/01/2161873.html)