C#--启动外部程序的几种方法
C# 启动外部程序的几种方法:
1. 启动外部程序,不等待其退出。
2. 启动外部程序,等待其退出。
3. 启动外部程序,无限等待其退出。
4. 启动外部程序,通过事件监视其退出。
出处:http://www.netcsharp.cn/showtopic-900.aspx
1. 启动外部程序,不等待其退出。
2. 启动外部程序,等待其退出。
3. 启动外部程序,无限等待其退出。
4. 启动外部程序,通过事件监视其退出。
1
// using System.Diagnostics;
2
private string appName = "calc.exe";
3
4
/// <summary>
5
/// 1. 启动外部程序,不等待其退出
6
/// </summary>
7
private void button1_Click(object sender, EventArgs e)
8
{
9
Process.Start(appName);
10
MessageBox.Show(String.Format("外部程序 {0} 启动完成!", this.appName), this.Text,
11
MessageBoxButtons.OK, MessageBoxIcon.Information);
12
}
13
14
/// <summary>
15
/// 2. 启动外部程序,等待其退出
16
/// </summary>
17
private void button2_Click(object sender, EventArgs e)
18
{
19
try
20
{
21
Process proc = Process.Start(appName);
22
if (proc != null)
23
{
24
proc.WaitForExit(3000);
25
if (proc.HasExited)
26
MessageBox.Show(String.Format("外部程序 {0} 已经退出!", this.appName), this.Text,
27
MessageBoxButtons.OK, MessageBoxIcon.Information);
28
else
29
{
30
// 如果外部程序没有结束运行则强行终止之。
31
proc.Kill();
32
MessageBox.Show(String.Format("外部程序 {0} 被强行终止!", this.appName), this.Text,
33
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
34
}
35
}
36
}
37
catch (ArgumentException ex)
38
{
39
MessageBox.Show(ex.Message, this.Text,
40
MessageBoxButtons.OK, MessageBoxIcon.Error);
41
}
42
}
43
44
45
/// <summary>
46
/// 3. 启动外部程序,无限等待其退出
47
/// </summary>
48
private void button3_Click(object sender, EventArgs e)
49
{
50
try
51
{
52
Process proc = Process.Start(appName);
53
if (proc != null)
54
{
55
proc.WaitForExit();
56
MessageBox.Show(String.Format("外部程序 {0} 已经退出!", this.appName), this.Text,
57
MessageBoxButtons.OK, MessageBoxIcon.Information);
58
}
59
}
60
catch (ArgumentException ex)
61
{
62
MessageBox.Show(ex.Message, this.Text,
63
MessageBoxButtons.OK, MessageBoxIcon.Error);
64
}
65
}
66
67
68
/// <summary>
69
/// 4. 启动外部程序,通过事件监视其退出
70
/// </summary>
71
private void button4_Click(object sender, EventArgs e)
72
{
73
try
74
{
75
// 启动外部程序
76
Process proc = Process.Start(appName);
77
if (proc != null)
78
{
79
// 监视进程退出
80
proc.EnableRaisingEvents = true;
81
// 指定退出事件方法
82
proc.Exited += new EventHandler(proc_Exited);
83
}
84
}
85
catch (ArgumentException ex)
86
{
87
MessageBox.Show(ex.Message, this.Text,
88
MessageBoxButtons.OK, MessageBoxIcon.Error);
89
}
90
}
91
92
/// <summary>
93
/// 外部程序退出事件
94
/// </summary>
95
void proc_Exited(object sender, EventArgs e)
96
{
97
MessageBox.Show(String.Format("外部程序 {0} 已经退出!", this.appName), this.Text,
98
MessageBoxButtons.OK, MessageBoxIcon.Information);
99
}
// using System.Diagnostics;2
private string appName = "calc.exe";3

4
/// <summary>5
/// 1. 启动外部程序,不等待其退出6
/// </summary>7
private void button1_Click(object sender, EventArgs e)8
{9
Process.Start(appName);10
MessageBox.Show(String.Format("外部程序 {0} 启动完成!", this.appName), this.Text, 11
MessageBoxButtons.OK, MessageBoxIcon.Information);12
}13

14
/// <summary>15
/// 2. 启动外部程序,等待其退出16
/// </summary>17
private void button2_Click(object sender, EventArgs e)18
{19
try20
{21
Process proc = Process.Start(appName);22
if (proc != null)23
{24
proc.WaitForExit(3000);25
if (proc.HasExited)26
MessageBox.Show(String.Format("外部程序 {0} 已经退出!", this.appName), this.Text, 27
MessageBoxButtons.OK, MessageBoxIcon.Information);28
else29
{30
// 如果外部程序没有结束运行则强行终止之。31
proc.Kill();32
MessageBox.Show(String.Format("外部程序 {0} 被强行终止!", this.appName), this.Text, 33
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);34
}35
}36
}37
catch (ArgumentException ex)38
{39
MessageBox.Show(ex.Message, this.Text, 40
MessageBoxButtons.OK, MessageBoxIcon.Error);41
}42
}43

44

45
/// <summary>46
/// 3. 启动外部程序,无限等待其退出47
/// </summary>48
private void button3_Click(object sender, EventArgs e)49
{50
try51
{52
Process proc = Process.Start(appName);53
if (proc != null)54
{55
proc.WaitForExit();56
MessageBox.Show(String.Format("外部程序 {0} 已经退出!", this.appName), this.Text, 57
MessageBoxButtons.OK, MessageBoxIcon.Information);58
}59
}60
catch (ArgumentException ex)61
{62
MessageBox.Show(ex.Message, this.Text, 63
MessageBoxButtons.OK, MessageBoxIcon.Error);64
}65
}66

67

68
/// <summary>69
/// 4. 启动外部程序,通过事件监视其退出70
/// </summary>71
private void button4_Click(object sender, EventArgs e)72
{73
try74
{75
// 启动外部程序76
Process proc = Process.Start(appName);77
if (proc != null)78
{79
// 监视进程退出80
proc.EnableRaisingEvents = true;81
// 指定退出事件方法82
proc.Exited += new EventHandler(proc_Exited);83
}84
}85
catch (ArgumentException ex)86
{87
MessageBox.Show(ex.Message, this.Text, 88
MessageBoxButtons.OK, MessageBoxIcon.Error);89
}90
}91
92
/// <summary>93
/// 外部程序退出事件94
/// </summary>95
void proc_Exited(object sender, EventArgs e)96
{97
MessageBox.Show(String.Format("外部程序 {0} 已经退出!", this.appName), this.Text, 98
MessageBoxButtons.OK, MessageBoxIcon.Information);99
} 

