100165803.htm
来自「C#高级编程(第三版),顶死你们。。 。up」· HTM 代码 · 共 708 行 · 第 1/3 页
HTM
708 行
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
32.4.6 安装程序
</title></head>
<body>
<div class="area">
<div class="col1">
<div class="lineBlue">
</div>
<!-- title -->
<div class="arcTitle">
<h1>
<a href="../16">
C#高级编程(第3版)【全文连载】
</a>
</h1>
<div style="text-align: center; font-size: 15px">
<a href="100165803.htm">
32.4.6 安装程序
</a>
</div>
<div style="text-align: center; font-size: 15px">
<a class="url" href="../../default.htm">http://book.csdn.net/</a>
2006-10-13 14:41:00
</div>
<div style="margin: 0px auto; width: 700px; border: solid 1px #0b5f98;">
<div style="float: left; width: 16px; background-color: #0b5f98; color: White; padding: 1px;">
图书导读
</div>
<div style="float: right; width: 670px; text-align: left; line-height: 16pt; padding-left: 2px">
<!--导读-->
<h1 id="divCurrentNode" style="color: #b83507; width: 100%; text-align: left; font-size: 12px; padding-left: 2px">当前章节:<a href='100165803.htm'><font color='red'>32.4.6 安装程序</font></a></h1>
<div id="divRelateNode" style="padding-left: 2px">
<div style='float:left;width:49%'>·<a href='100165800.htm'>32.4.3 Windows服务项目</a></div><div style='float:right;width:49%'>·<a href='100165801.htm'>32.4.4 线程和服务</a></div><div style='float:left;width:49%'>·<a href='100165802.htm'>32.4.5 服务的安装</a></div><div style='float:right;width:49%'>·<a href='100165804.htm'>32.5 服务的监视和控制</a></div><div style='float:left;width:49%'>·<a href='100165805.htm'>32.5.1 MMC计算机管理</a></div><div style='float:right;width:49%'>·<a href='100165806.htm'>32.5.2 net.exe</a></div></div>
</div>
</div>
</div>
<!-- main -->
<div id="main">
<div id="text"> <link href="css.css" rel="stylesheet" type="text/css" /><h3 style='margin-top:8.15pt;margin-right:0cm;FTEL:8.15pt;margin-left:
0cm'><a FTEL="_Toc507815583"><span lang=EN-US>32.4.6 </span></a><span
style='font-family:黑体'>安装程序</span></h3>
<p class=MsoNormal style='line-height:17.0pt'><span style='font-family:宋体'>切换到</span><span
lang=EN-US>Visual Studio .NET</span><span style='font-family:宋体'>的设计视图,从上下文菜单中选择</span><span
lang=EN-US>Add Installer</span><span style='font-family:宋体'>选项,可以给服务添加安装程序。使用</span><span
lang=EN-US>Add Installer</span><span style='font-family:宋体'>选项时,将创建一个新的</span><span
lang=EN-US>ProjectInstaller</span><span style='font-family:宋体'>类、一个</span><span
lang=EN-US>ServiceInstaller</span><span style='font-family:宋体'>实例和一个</span><span
lang=EN-US>ServiceProcessInstaller</span><span style='font-family:宋体'>实例。</span></p>
<p class=MsoNormal style='FTEL:4.9pt;line-height:17.0pt'><span
style='font-family:宋体'>图</span><span lang=EN-US>32-11</span><span
style='font-family:宋体'>显示的是服务的安装程序类。</span></p>
<p align=center><span style='position:absolute;z-index:10;left:0px;margin-left:
140px;margin-top:3px;width:323px;height:343px'><img width=323 height=343
src="32/image011.gif"></span><span lang=EN-US><img width=342
height=344 src="32/image012.jpg"></span></p>
<p align=center style='FTEL:8.15pt'><span style='font-family:宋体;color:black'>图</span><span
lang=EN-US style='color:black'> 32-11</span></p>
<p class=MsoNormal><span style='font-family:宋体'>根据这张图表,下面详细讨论由</span><span
lang=EN-US>Add Installer</span><span style='font-family:宋体'>选项创建的</span><span
lang=EN-US>ProjectInstaller.cs</span><span style='font-family:宋体'>文件中的源代码。</span></p>
<h4 style='FTEL:21.45pt'><span lang=EN-US>1. </span><span
style='font-family:黑体'>安装程序类</span></h4>
<p class=MsoNormal style='line-height:15.0pt'><span lang=EN-US>ProjectInstaller</span><span
style='font-family:宋体'>类是从</span><span lang=EN-US>System.Configuration.Install.Installer</span><span
style='font-family:宋体'>派生出来的,它是所有定制安装程序类的基类。使用</span><span lang=EN-US>Installer</span><span
style='font-family:宋体'>类,可以创建基于事务的安装。使用基于事务的安装时,如果安装失败了,系统还可以回滚到以前的状态,安装程序所做的所有修改都会被取消。如图</span><span
lang=EN-US>32-11</span><span style='font-family:宋体'>所示,</span><span lang=EN-US>Installer</span><span
style='font-family:宋体'>类中有</span><span lang=EN-US>Install()</span><span
style='font-family:宋体'>、</span><span lang=EN-US>Commit()</span><span
style='font-family:宋体'>、</span><span lang=EN-US>Rollback()</span><span
style='font-family:宋体'>和</span><span lang=EN-US>Uninstall()</span><span
style='font-family:宋体'>方法,这些方法都是从安装程序中调用的。</span></p>
<p class=MsoNormal style='line-height:15.0pt'><span style='font-family:宋体;
color:black'>如果</span><span lang=EN-US style='color:black'>RunInstaller</span><span
style='font-family:宋体;color:black'>属性的值为</span><span lang=EN-US
style='color:black'>true</span><span style='font-family:宋体;color:black'>,则在安装程序集时就会调用</span><span
lang=EN-US style='color:black'>ProjectInstaller</span><span style='font-family:
宋体;color:black'>类。定制的安装程序和</span><span lang=EN-US style='color:black'>installutil.exe(</span><span
style='font-family:宋体;color:black'>这个程序以后将用到</span><span lang=EN-US
style='color:black'>)</span><span style='font-family:宋体;color:black'>都能检查该属性。</span></p>
<p class=2 style='margin-top:6.5pt;margin-right:0cm;FTEL:0cm;
margin-left:21.45pt;FTEL:.0001pt;FTEL:18.45pt'><span
lang=EN-US>using System;</span></p>
<p class=2 style='margin-left:21.45pt;FTEL:18.45pt;line-height:13.0pt'><span
lang=EN-US>using System.Collections;</span></p>
<p class=2 style='margin-left:21.45pt;FTEL:18.45pt;line-height:13.0pt'><span
lang=EN-US>using System.ComponentModel;</span></p>
<p class=2 style='margin-left:21.45pt;FTEL:18.45pt;line-height:13.0pt'><span
lang=EN-US>using System.Configuration.Install;</span></p>
<p class=2 style='margin-left:21.45pt;FTEL:18.45pt;line-height:13.0pt'><span
lang=EN-US> </span></p>
<p class=2 style='margin-left:21.45pt;FTEL:18.45pt;line-height:13.0pt'><span
lang=EN-US>namespace Wrox.ProCSharp.WinServices</span></p>
<p class=2 style='margin-left:21.45pt;FTEL:18.45pt;line-height:13.0pt'><span
lang=EN-US>{</span></p>
<p class=2 style='margin-left:21.45pt;FTEL:18.45pt;line-height:13.0pt'><span
lang=EN-US>/// <summary></span></p>
<p class=2 style='margin-left:21.45pt;FTEL:18.45pt;line-height:13.0pt'><span
lang=EN-US> /// Summary description for
ProjectInstaller.</span></p>
<p class=2 style='margin-left:21.45pt;FTEL:18.45pt;line-height:13.0pt'><span
lang=EN-US> /// </summary></span></p>
<p class=2 style='margin-left:21.45pt;FTEL:18.45pt;line-height:13.0pt'><span
lang=EN-US> </span></p>
<p class=2 style='margin-left:21.45pt;FTEL:18.45pt;line-height:13.0pt'><span
lang=EN-US> [RunInstaller(true)]</span></p>
<p class=2 style='margin-left:21.45pt;FTEL:18.45pt;line-height:13.0pt'><span
lang=EN-US> public class ProjectInstaller :
System.Configuration.Install.Installer</span></p>
<p class=2 style='margin-top:0cm;margin-right:0cm;FTEL:8.15pt;
margin-left:21.45pt;FTEL:18.45pt'><span lang=EN-US> {</span></p>
<h4 style='FTEL:21.45pt'><span lang=EN-US>2. ServiceProcessInstaller</span><span
style='font-family:黑体'>类和</span><span lang=EN-US>ServiceInstaller</span><span
style='font-family:黑体'>类</span></h4>
<p class=MsoNormal><span style='font-family:宋体;color:black'>与</span><span
lang=EN-US style='color:black'>Windows Forms</span><span style='font-family:
宋体;color:black'>应用程序类似,</span><span lang=EN-US style='color:black'>InitializeComponent()</span><span
style='font-family:宋体;color:black'>是在</span><span lang=EN-US style='color:black'>ProjectInstaller</span><span
style='font-family:宋体;color:black'>类的构造函数中调用的。在</span><span lang=EN-US
style='color:black'>InitializeComponent()</span><span style='font-family:宋体;
color:black'>中,创建了</span><span lang=EN-US style='color:black'>ServiceProcessInstaller</span><span
style='font-family:宋体;color:black'>类和</span><span lang=EN-US style='color:black'>ServiceInstaller</span><span
style='font-family:宋体;color:black'>类的实例。这两个类都是从</span><span lang=EN-US
style='color:black'>ComponentInstaller</span><span style='font-family:宋体;
color:black'>类中派生出来的,</span><span lang=EN-US style='color:black'>ComponentInstaller</span><span
style='font-family:宋体;color:black'>类本身派生于</span><span lang=EN-US
style='color:black'>Installer</span><span style='font-family:宋体;color:black'>。</span></p>
<p class=MsoNormal><span style='font-family:宋体'>从</span><span lang=EN-US>ComponentInstaller</span><span
style='font-family:宋体'>中派生出来的类可以用作安装进程的一个部分。记住,一个服务进程可以包括多个服务。</span><span
lang=EN-US>ServiceProcessInstaller</span><span style='font-family:宋体'>类用于配置进程,为这个进程中所有服务定义值,而</span><span
lang=EN-US>ServiceInstaller</span><span style='font-family:宋体'>类用于服务的配置,因此,每个服务都需要</span><span
lang=EN-US>ServiceInstaller</span><span style='font-family:宋体'>类的一个实例。如果进程中有</span><span
lang=EN-US>3</span><span style='font-family:宋体'>个服务,则必须添加额外的</span><span
lang=EN-US>ServiceInstaller</span><span style='font-family:宋体'>对象,也就是说,在这种情况下,需要</span><span
lang=EN-US>3</span><span style='font-family:宋体'>个</span><span lang=EN-US>ServiceInstaller</span><span
style='font-family:宋体'>实例:</span></p>
<p class=2 style='margin-top:8.15pt;margin-right:0cm;FTEL:0cm;
margin-left:21.45pt;FTEL:.0001pt;FTEL:18.45pt'><span
lang=EN-US> private
System.ServiceProcess.ServiceProcessInstaller</span></p>
<p class=2 style='margin-left:21.45pt;FTEL:18.45pt;line-height:15.0pt'><span
lang=EN-US>
serviceProcessInstaller1;</span></p>
<p class=2 style='margin-left:21.45pt;FTEL:18.45pt;line-height:15.0pt'><span
lang=EN-US> private
System.ServiceProcess.ServiceInstaller serviceInstaller1;</span></p>
<p class=2 style='margin-left:21.45pt;FTEL:18.45pt;line-height:15.0pt'><span
lang=EN-US> </span></p>
<p class=2 style='margin-left:21.45pt;FTEL:18.45pt;line-height:15.0pt'><span
lang=EN-US> /// <summary></span></p>
<p class=2 style='margin-left:21.45pt;FTEL:18.45pt;line-height:15.0pt'><span
lang=EN-US> /// Required
designer variable.</span></p>
<p class=2 style='margin-left:21.45pt;FTEL:18.45pt;line-height:15.0pt'><span
lang=EN-US> /// </summary></span></p>
<p class=2 style='margin-left:21.45pt;FTEL:18.45pt;line-height:15.0pt'><span
lang=EN-US> </span></p>
<p class=2 style='margin-left:21.45pt;FTEL:18.45pt;line-height:15.0pt'><span
lang=EN-US> private
System.ComponentModel.Container components = null;</span></p>
<p class=2 style='margin-left:21.45pt;FTEL:18.45pt;line-height:15.0pt'><span
lang=EN-US> public ProjectInstaller()</span></p>
<p class=2 style='margin-left:21.45pt;FTEL:18.45pt;line-height:15.0pt'><span
lang=EN-US> {</span></p>
<p class=2 style='margin-left:21.45pt;FTEL:18.45pt;line-height:15.0pt'><span
lang=EN-US> // This call is
required by the Designer.</span></p>
<p class=2 style='margin-left:21.45pt;FTEL:18.45pt;line-height:15.0pt'><span
lang=EN-US>
InitializeComponent();</span></p>
<p class=2 style='margin-left:21.45pt;FTEL:18.45pt;line-height:15.0pt'><span
lang=EN-US> </span></p>
<p class=2 style='margin-left:21.45pt;FTEL:18.45pt;line-height:15.0pt'><span
lang=EN-US> // TODO: Add any
initialization after the InitComponent call</span></p>
<p class=2 style='margin-left:21.45pt;FTEL:18.45pt;line-height:15.0pt'><span
lang=EN-US> }</span></p>
<p class=2 style='margin-left:21.45pt;FTEL:18.45pt;line-height:15.0pt'><span
lang=EN-US> /// <summary></span></p>
<p class=2 style='margin-left:21.45pt;FTEL:18.45pt;line-height:15.0pt'><span
lang=EN-US> /// Required method
for Designer support - do not modify</span></p>
<p class=2 style='margin-left:21.45pt;FTEL:18.45pt;line-height:15.0pt'><span
lang=EN-US> /// the contents of
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?