转自:http://blog.youkuaiyun.com/myhuli120/article/details/6927588
一、自动更新的实现
让客户端实现自动更新,通常做法是在客户端部署一个单独的自动更新程序。主程序启动后,访问服务端,检查配置文件是
否有更新版本,有更新版本就启动更新程序,由更新负责下载更新版本,并更新客户端程序,流程如下:
1)版本判断:
客户端和服务端都部署同一个版本文件,客户端登陆时发送验证给服务端判断版本是否一致。
Version.xml代码
<span style="color: blue;"><</span><span style="color: rgb(163, 21, 21);">iq </span><span style="color: red;">xmlns</span><span style="color: blue;">=</span>"<span style="color: blue;">http://www.dynastech.com/xmtp</span>" <span style="color: red;">from</span><span style="color: blue;">=</span>"<span style="color: blue;">*@domcool.local/updater</span>" <span style="color: red;">to</span><span style="color: blue;">=</span>"<span style="color: blue;">*@domcool.local/updater</span>" <span style="color: red;">type</span><span style="color: blue;">=</span>"<span style="color: blue;">get</span>"
<span style="color: red;">id</span><span style="color: blue;">=</span>"<span style="color: blue;">508f3e88-4bb0-4585-a5c6-cc41ef57fef3</span>"<span style="color: blue;">> <</span><span style="color: rgb(163, 21, 21);">query </span><span style="color: red;">xmlns</span><span style="color: blue;">=</span>"<span style="color: blue;">http://www.dynastech.com/xmtp/disco#update</span>" <span style="color: red;">version</span><span style="color: blue;">=</span>"<span style="color: blue;">20090922</span>" <span style="color: red;">lastUpdateTime</span><span style="color: blue;">=</span>"<span style="color: blue;">2009-09-22</span>"
<span style="color: red;">fileUrl</span><span style="color: blue;">=</span>"<span style="color: blue;"><a target=_blank href="http://172.0.0.1/UCCompanion/UCCompanionSetup(0922).zip%22%3E" style="color: black;"><span style="color: rgb(20, 99, 196);">http://172.0.0.1/UCCompanion/UCCompanionSetup(0922).zip</span></a> <</span>"<span style="color: blue;">> </span><span style="color: rgb(163, 21, 21);">x </span><span style="color: red;">xmlns</span><span style="color: blue;">=</span>"<span style="color: blue;">http://www.dynastech.com/xmtp/item</span>"<span style="color: blue;">> </</span><span style="color: rgb(163, 21, 21);">x</span><span style="color: blue;">> </</span><span style="color: rgb(163, 21, 21);">query</span><span style="color: blue;">> </</span><span style="color: rgb(163, 21, 21);">iq</span><span style="color: blue;">></span>
版本文件主要比较服务端Version.xml文件和客户端Version.xml文件中Version(版本号)是否一致,如果服务端Version属性
大于客户端的Version属性,则通过服务端的fileUrl属性获取新版本的下载地址。供更新程序使用。
2)删除原有更新包
所有客户端更新文件均下载到C:\Documents and Settings\当前用户名\Local Settings\Temp 文件夹内,当客户端运行后首先判
断是否有新更新包需要下载,如果没有则判断该临时文件夹内是否有旧有安装文件,如果存在,则删除旧有安装文件。
<span style="color: blue;">private void </span>RemoveOldSetupFile() { <span style="color: blue;">try </span>{ <span style="color: blue;">string </span>temp = System.<span style="color: rgb(43, 145, 175);">Environment</span>.GetEnvironmentVariable(<span style="color: rgb(163, 21, 21);">"TEMP"</span>); <span style="color: blue;">string </span>folder = <span style="color: blue;">new </span><span style="color: rgb(43, 145, 175);">DirectoryInfo</span>(temp).FullName; <span style="color: blue;">if </span>(<span style="color: rgb(43, 145, 175);">File</span>.Exists(folder + <span style="color: rgb(163, 21, 21);">@"\" </span>+ setupName + <span style="color: rgb(163, 21, 21);">".exe"</span>)) { <span style="color: rgb(43, 145, 175);">File</span>.Delete(folder + <span style="color: rgb(163, 21, 21);">@"\" </span>+ setupName + <span style="color: rgb(163, 21, 21);">".exe"</span>); } <span style="color: blue;">if </span>(<span style="color: rgb(43, 145, 175);">File</span>.Exists(folder + <span style="color: rgb(163, 21, 21);">@"\" </span>+ setupName + <span style="color: rgb(163, 21, 21);">".msi"</span>)) { <span style="color: rgb(43, 145, 175);">File</span>.Delete(folder + <span style="color: rgb(163, 21, 21);">@"\" </span>+ setupName + <span style="color: rgb(163, 21, 21);">".msi"</span>); } } <span style="color: blue;">catch </span>{ } }
备注:关于获取系统特殊文件夹的方法见博客http://www.cnblogs.com/thornfield_he/archive/2009/09/22/1571719.html
3)启动下载程序
下载程序和客户端程序是相互独立的,可以通过客户端开启新线程启动下载程序。下载程序在文件下载结束后可以关掉客户端程序,
并开启新线程启动安装程序进行安装。
<span style="color: blue;">private void </span>Update() { <span style="color: blue;">if </span>(ShouldUpdate(query.Version, <span style="color: blue;">this</span>.version)) { <span style="color: rgb(43, 145, 175);">MessageBox</span>.Show(<span style="color: rgb(163, 21, 21);">"请更新客户端文件到版本[" </span>+ query.Version + <span style="color: rgb(163, 21, 21);">"]"</span>, <span style="color: rgb(163, 21, 21);">"更新提示"</span>, <span style="color: rgb(43, 145, 175);">MessageBoxButtons</span>.OK,
<span style="color: rgb(43, 145, 175);">MessageBoxIcon</span>.Asterisk); System.Diagnostics.<span style="color: rgb(43, 145, 175);">Process</span>.Start(<span style="color: rgb(43, 145, 175);">Application</span>.StartupPath + <span style="color: rgb(163, 21, 21);">@"\AutoUpdater.exe"</span>, query.FileUrl); } <span style="color: blue;">else </span>{ RemoveOldSetupFile(); } } <span style="color: blue;">private bool </span>ShouldUpdate(<span style="color: blue;">string </span>serverVersion, <span style="color: blue;">string </span>localVersion) { <span style="color: blue;">if </span>(!<span style="color: blue;">string</span>.IsNullOrEmpty(serverVersion) && !<span style="color: blue;">string</span>.IsNullOrEmpty(localVersion)) { <span style="color: blue;">return </span>serverVersion.CompareTo(localVersion) > 0; } <span style="color: blue;">return true</span>; }
调用AutoUpdater.exe文件时需要传入文件下载地址。
System.Diagnostics.<span style="color: rgb(43, 145, 175);">Process</span>.Start(<span style="color: rgb(43, 145, 175);">Application</span>.StartupPath + <span style="color: rgb(163, 21, 21);">@"\AutoUpdater.exe"</span>, query.FileUrl);
4)下载程序代码
下载程序界面
<a target=_blank href="http://images.cnblogs.com/cnblogs_com/thornfield_he/WindowsLiveWriter/8645c7345ec9_9E9A/image_20.png" style="color: black;"><img title="image" border="0" alt="image" src="http://images.cnblogs.com/cnblogs_com/thornfield_he/WindowsLiveWriter/8645c7345ec9_9E9A/image_thumb_9.png" width="312" height="95" style="border: 0px; max-width: 100%; display: inline;" /></a>
<span style="color: blue;">using </span>System; <span style="color: blue;">using </span>System.Collections.Generic; <span style="color: blue;">using </span>System.ComponentModel; <span style="color: blue;">using </span>System.Data; <span style="color: blue;">using </span>System.Drawing; <span style="color: blue;">using </span>System.Text; <span style="color: blue;">using </span>System.Windows.Forms; <span style="color: blue;">using </span>System.Net; <span style="color: blue;">using </span>System.IO; <span style="color: blue;">using </span>System.Threading; <span style="color: blue;">using </span>System.Diagnostics; <span style="color: blue;">namespace </span>AutoUpdater { <span style="color: blue;">public partial class </span><span style="color: rgb(43, 145, 175);">MainForm </span>: <span style="color: rgb(43, 145, 175);">Form </span>{ <span style="color: blue;">private </span><span style="color: rgb(43, 145, 175);">WebClient </span>client; <span style="color: blue;">private string </span>URl; <span style="color: blue;">private string </span>fileName; <span style="color: blue;">private string </span>path; <span style="color: blue;">private const string </span>applicationFile = <span style="color: rgb(163, 21, 21);">"Setup"</span>; <span style="color: blue;">public </span>MainForm(<span style="color: blue;">string </span>url) { InitializeComponent(); <span style="color: blue;">this</span>.URl = url; client = <span style="color: blue;">new </span><span style="color: rgb(43, 145, 175);">WebClient</span>(); client.DownloadProgressChanged += <span style="color: blue;">new </span><span style="color: rgb(43, 145, 175);">DownloadProgressChangedEventHandler</span>(client_DownloadProgressChanged); client.DownloadFileCompleted += <span style="color: blue;">new </span><span style="color: rgb(43, 145, 175);">AsyncCompletedEventHandler</span>(client_DownloadFileCompleted); client.Proxy = <span style="color: rgb(43, 145, 175);">WebRequest</span>.DefaultWebProxy; client.Proxy.Credentials = <span style="color: blue;">new </span><span style="color: rgb(43, 145, 175);">NetworkCredential</span>(); <span style="color: blue;">this</span>.Hide(); <span style="color: green;">//Thread thread = new Thread(UpdateFile); //Thread.Sleep(15000); //thread.Start(); </span>UpdateFile(); } <span style="color: blue;">public </span>MainForm() { InitializeComponent(); } <span style="color: gray;">/// <summary> /// </span><span style="color: green;">下载完成调用 </span><span style="color: gray;">/// </summary> /// <param name="sender"></param> /// <param name="e"></param> </span><span style="color: blue;">void </span>client_DownloadFileCompleted(<span style="color: blue;">object </span>sender, <span style="color: rgb(43, 145, 175);">AsyncCompletedEventArgs </span>e) { label1.Text = <span style="color: rgb(163, 21, 21);">"文件接收完成"</span>; UnZip(); RunUpdate(); } <span style="color: gray;">/// <summary> /// </span><span style="color: green;">下载进度条 </span><span style="color: gray;">/// </summary> /// <param name="sender"></param> /// <param name="e"></param> </span><span style="color: blue;">void </span>client_DownloadProgressChanged(<span style="color: blue;">object </span>sender, <span style="color: rgb(43, 145, 175);">DownloadProgressChangedEventArgs </span>e) { <span style="color: blue;">this</span>.progressBar1.Value = e.ProgressPercentage; } <span style="color: gray;">/// <summary> /// </span><span style="color: green;">开始下载 </span><span style="color: gray;">/// </summary> </span><span style="color: blue;">private void </span>StartDownload() { fileName = URl.Substring(URl.LastIndexOf(<span style="color: rgb(163, 21, 21);">"/"</span>) + 1, URl.Length - URl.LastIndexOf(<span style="color: rgb(163, 21, 21);">"/"</span>) - 1); path = GetTempFolder(); <span style="color: blue;">try </span>{ <span style="color: rgb(43, 145, 175);">WebRequest </span>myre = <span style="color: rgb(43, 145, 175);">WebRequest</span>.Create(URl); } <span style="color: blue;">catch </span>(<span style="color: rgb(43, 145, 175);">Exception </span>ex) { <span style="color: rgb(43, 145, 175);">MessageBox</span>.Show(ex.Message, <span style="color: rgb(163, 21, 21);">"Error"</span>); } <span style="color: blue;">try </span>{ label1.Text = <span style="color: rgb(163, 21, 21);">"开始下载文件..."</span>; client.DownloadFileAsync(<span style="color: blue;">new </span><span style="color: rgb(43, 145, 175);">Uri</span>(URl), path + <span style="color: rgb(163, 21, 21);">@"\" </span>+ fileName); } <span style="color: blue;">catch </span>(<span style="color: rgb(43, 145, 175);">WebException </span>exp) { label1.Text = exp.Message; } } <span style="color: gray;">/// <summary> /// </span><span style="color: green;">解压压缩包,格式必须是*.zip,否则不能解压 </span><span style="color: gray;">/// </span><span style="color: green;">因为是调用Windows内部api进行解压,只能够识别zip压缩包 </span><span style="color: gray;">/// </span><span style="color: green;">必须添加C:\WINDOWS\system32\shell32.dll的引用 </span><span style="color: gray;">/// </summary> </span><span style="color: blue;">private void </span>UnZip() { <span style="color: blue;">try </span>{ Shell32.<span style="color: rgb(43, 145, 175);">ShellClass </span>sc = <span style="color: blue;">new </span>Shell32.<span style="color: rgb(43, 145, 175);">ShellClass</span>(); Shell32.<span style="color: rgb(43, 145, 175);">Folder </span>SrcFolder = sc.NameSpace(<span style="color: blue;">this</span>.path + <span style="color: rgb(163, 21, 21);">@"\" </span>+ <span style="color: blue;">this</span>.fileName); Shell32.<span style="color: rgb(43, 145, 175);">Folder </span>DestFolder = sc.NameSpace(<span style="color: blue;">this</span>.path); Shell32.<span style="color: rgb(43, 145, 175);">FolderItems </span>items = SrcFolder.Items(); DestFolder.CopyHere(items, 20); } <span style="color: blue;">catch </span>(<span style="color: rgb(43, 145, 175);">Exception </span>ex) { <span style="color: rgb(43, 145, 175);">MessageBox</span>.Show(ex.Message); } } <span style="color: gray;">/// <summary> /// </span><span style="color: green;">获取下载文件夹地址及解压文件存放地址 </span><span style="color: gray;">/// </span><span style="color: green;">此地址默认为C:\Documents and Settings\当前用户名\Local Settings\Temp 文件夹 </span><span style="color: gray;">/// </summary> /// <returns></returns> </span><span style="color: blue;">private string </span>GetTempFolder() { <span style="color: blue;">string </span>folder = System.<span style="color: rgb(43, 145, 175);">Environment</span>.GetEnvironmentVariable(<span style="color: rgb(163, 21, 21);">"TEMP"</span>); <span style="color: blue;">return new </span><span style="color: rgb(43, 145, 175);">DirectoryInfo</span>(folder).FullName; } <span style="color: gray;">/// <summary> /// </span><span style="color: green;">开始下载文件 </span><span style="color: gray;">/// </summary> </span><span style="color: blue;">private void </span>UpdateFile() { <span style="color: blue;">this</span>.Hide(); <span style="color: green;">//如果临时文件夹存在setup安装文件,就直接调用安装文件 </span><span style="color: blue;">if </span>(<span style="color: rgb(43, 145, 175);">File</span>.Exists(GetTempFolder() + <span style="color: rgb(163, 21, 21);">@"\" </span>+ applicationFile + <span style="color: rgb(163, 21, 21);">".exe"</span>) && <span style="color: rgb(43, 145, 175);">File</span>.Exists(GetTempFolder() +
<span style="color: rgb(163, 21, 21);">@"\" </span>+ applicationFile + <span style="color: rgb(163, 21, 21);">".msi"</span>)) { label1.Text = <span style="color: rgb(163, 21, 21);">"开始下载文件..."</span>; <span style="color: blue;">this</span>.progressBar1.Value = <span style="color: blue;">this</span>.progressBar1.Maximum; label1.Text = <span style="color: rgb(163, 21, 21);">"文件接收完成"</span>; RunUpdate(); } <span style="color: green;">//如果临时文件夹不存在setup安装文件,就从网络下载 </span><span style="color: blue;">else </span>{ RemoveSetupFile(); StartDownload(); } } <span style="color: gray;">/// <summary> /// </span><span style="color: green;">清除旧有已下载的安装文件 </span><span style="color: gray;">/// </summary> </span><span style="color: blue;">private static void </span>RemoveSetupFile() { <span style="color: blue;">try </span>{ <span style="color: blue;">string </span>temp = System.<span style="color: rgb(43, 145, 175);">Environment</span>.GetEnvironmentVariable(<span style="color: rgb(163, 21, 21);">"TEMP"</span>); <span style="color: blue;">string </span>folder = <span style="color: blue;">new </span><span style="color: rgb(43, 145, 175);">DirectoryInfo</span>(temp).FullName; <span style="color: blue;">if </span>(<span style="color: rgb(43, 145, 175);">File</span>.Exists(folder + <span style="color: rgb(163, 21, 21);">@"\" </span>+ applicationFile + <span style="color: rgb(163, 21, 21);">".exe"</span>)) { <span style="color: rgb(43, 145, 175);">File</span>.Delete(folder + <span style="color: rgb(163, 21, 21);">@"\" </span>+ applicationFile + <span style="color: rgb(163, 21, 21);">".exe"</span>); } <span style="color: blue;">if </span>(<span style="color: rgb(43, 145, 175);">File</span>.Exists(folder + <span style="color: rgb(163, 21, 21);">@"\" </span>+ applicationFile + <span style="color: rgb(163, 21, 21);">".msi"</span>)) { <span style="color: rgb(43, 145, 175);">File</span>.Delete(folder + <span style="color: rgb(163, 21, 21);">@"\" </span>+ applicationFile + <span style="color: rgb(163, 21, 21);">".msi"</span>); } } <span style="color: blue;">catch </span>{ } } <span style="color: gray;">/// <summary> /// </span><span style="color: green;">下载完毕,开始执行更新程序 </span><span style="color: gray;">/// </summary> </span><span style="color: blue;">private void </span>RunUpdate() { <span style="color: blue;">try </span>{ <span style="color: blue;">foreach </span>(<span style="color: rgb(43, 145, 175);">Process </span>p <span style="color: blue;">in </span><span style="color: rgb(43, 145, 175);">Process</span>.GetProcesses()) { <span style="color: blue;">if </span>(p.ProcessName.ToLower().StartsWith(<span style="color: rgb(163, 21, 21);">"uccompanion"</span>)) { <span style="color: blue;">if </span>(<span style="color: rgb(43, 145, 175);">MessageBox</span>.Show(<span style="color: rgb(163, 21, 21);">"UCCompanion正在运行,是否关闭当前程序安装更新?"</span>, <span style="color: rgb(163, 21, 21);">"安装UCCompanion"</span>,
<span style="color: rgb(43, 145, 175);">MessageBoxButtons</span>.YesNo, <span style="color: rgb(43, 145, 175);">MessageBoxIcon</span>.Question) == <span style="color: rgb(43, 145, 175);">DialogResult</span>.Yes) { p.Kill(); <span style="color: rgb(43, 145, 175);">Process</span>.Start(GetTempFolder() + <span style="color: rgb(163, 21, 21);">@"\" </span>+ applicationFile + <span style="color: rgb(163, 21, 21);">".exe"</span>); } <span style="color: blue;">else </span>{ <span style="color: rgb(43, 145, 175);">MessageBox</span>.Show(<span style="color: rgb(163, 21, 21);">"UCCompanion下载完成,将在下次启动时提醒更新!"</span>); } } } } <span style="color: blue;">catch </span>(<span style="color: rgb(43, 145, 175);">Exception </span>ex) { <span style="color: rgb(43, 145, 175);">MessageBox</span>.Show(ex.Message); } <span style="color: blue;">finally </span>{ <span style="color: blue;">this</span>.Close(); } } <span style="color: gray;">/// <summary> /// </span><span style="color: green;">重载WindProc判断点击关闭按钮(X)时,隐藏程序界面 </span><span style="color: gray;">/// </summary> /// <param name="msg"></param> </span><span style="color: blue;">protected override void </span>WndProc(<span style="color: blue;">ref </span><span style="color: rgb(43, 145, 175);">Message </span>msg) { <span style="color: blue;">const int </span>WM_SYSCOMMAND = 0x0112; <span style="color: blue;">const int </span>SC_CLOSE = 0xF060; <span style="color: blue;">if </span>(msg.Msg == WM_SYSCOMMAND && ((<span style="color: blue;">int</span>)msg.WParam == SC_CLOSE)) { <span style="color: blue;">this</span>.Hide(); <span style="color: blue;">return</span>; } <span style="color: blue;">base</span>.WndProc(<span style="color: blue;">ref </span>msg); } <span style="color: gray;">/// <summary> /// </span><span style="color: green;">双击图标弹出界面 </span><span style="color: gray;">/// </summary> /// <param name="sender"></param> /// <param name="e"></param> </span><span style="color: blue;">private void </span>icon_notify_MouseDoubleClick(<span style="color: blue;">object </span>sender, <span style="color: rgb(43, 145, 175);">MouseEventArgs </span>e) { <span style="color: blue;">this</span>.Show(); <span style="color: blue;">this</span>.WindowState = <span style="color: rgb(43, 145, 175);">FormWindowState</span>.Normal; } <span style="color: gray;">/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> </span><span style="color: blue;">private void </span>MainForm_SizeChanged(<span style="color: blue;">object </span>sender, <span style="color: rgb(43, 145, 175);">EventArgs </span>e) { <span style="color: blue;">if </span>(<span style="color: blue;">this</span>.WindowState == <span style="color: rgb(43, 145, 175);">FormWindowState</span>.Minimized) { <span style="color: blue;">this</span>.Hide(); } } <span style="color: blue;">private void </span>MainForm_Load(<span style="color: blue;">object </span>sender, <span style="color: rgb(43, 145, 175);">EventArgs </span>e) { <span style="color: blue;">this</span>.Hide(); } } <span style="color: blue;">static class </span><span style="color: rgb(43, 145, 175);">Program </span>{ <span style="color: gray;">/// <summary> /// </span><span style="color: green;">启动,接收传入网址作为参数 </span><span style="color: gray;">/// </summary> /// <param name="agr"></param> </span>[<span style="color: rgb(43, 145, 175);">STAThread</span>] <span style="color: blue;">static void </span>Main(<span style="color: blue;">string</span>[] agr) { <span style="color: blue;">if </span>(agr.Length == 1 && agr[0].StartsWith(<span style="color: rgb(163, 21, 21);">@"http://"</span>)) { <span style="color: rgb(43, 145, 175);">MainForm </span>form = <span style="color: blue;">new </span><span style="color: rgb(43, 145, 175);">MainForm</span>(agr[0]); <span style="color: rgb(43, 145, 175);">Application</span>.Run(form); } } } }
将AutoUpdater项目生成的文件添加到客户端文件中,在客户端的Update()方法里调用updater,实现更新文件的下载。
以上就已经实现了自动更新功能,下面将讨论文件安装包的制作。
二、安装包的制作
1)创建安装项目
2)鼠标右击Setup项目选择>视图,可以看到制作安装包常见的视图有以下几个
最常用的视图有“文件系统”,“用户界面”和“启动条件”。
3)指定安装属性
鼠标左键单击项目名称,记住是左键单击,然后点击属性标签,注意:不是右击的属性
a.需要注意的是Version属性,每次版本更新时Version值必须后面的版本大于前面的版本。每次更改Version值时Projectcode会更改一次。
其中你修改安装项目的版本号时,比如从v1.00 到1.01,在你再次生成项目的时候,会提示你是否允许修改ProductCode,选择"是",
程序会自动修改ProductCode,选择否将保持相同的ProductCode,即不能自动卸载旧的版本.
b.在以后版本中要确认和以前的版本两个版本有不同的ProductCode和相同的UpgradeCode
c.manufacturer属性指定制造商名称。
d.detectnewerinstalledversion属性选择为true,
e.removepreviousversions选择为true
鼠标左键单击项目名称,此次是右键单击,然后点击属性,弹出属性页,选择“系统必备”。
在打开的系统必备页中,选中如下中的选择项,这个很重要!!!!!1!!!!!选上以后,在生成的安装文件包中
包含.netframework组件.(这个选项默认是没有选中的)。
4)文件系统视图
文件系统视图左侧根目录树下有3个子节点。
a.应用程序文件夹:将所有待打包的应用程序的可执行文件和相应的类库和组件拖动到该目录下。该目录可以创建子
目录,项目安装完毕以后的文件夹结构会和该目录下结构一致。
如图:
然后右击左边的"应用程序文件夹"打开属性对话框,修改文件释放路径,[ProgramFilesFolder][Manufacturer]\[ProductName]。
安装程序默认安装目录会是"c:\programm file\制造商名称\安装解决方案名称";
b.用户的“程序”菜单和用户桌面:用于在开始菜单创建文件快捷方式
在应用程序文件夹中将需要生成的快捷方式的文件添加快捷方式并拖动到用户的“程序”菜单和用户桌面
c.添加文件卸载功能
在添加你的应用程序项目的时候,多添加一个msiexec.exe进去,这个文件在c:\windows\system32文件夹下。
为其在程序菜单添加一个快捷方式,把他的名字改成"Uninstall.exe",指定Icon快捷方式显示的图标。然后下面我们
要的做的就是查找这个部署项目的ProductCode了,鼠标左键单击项目名称,记住是左键单击,然后点击属性标签,注意:
不是右击的属性,这个区别很大,这时你就可以看到ProductCode了
然后打开你创建的那个卸载程序的快捷方式的属性对话框,在Aguements属性中输入"/x {ProductCode}"
5)用户界面视图
在“欢迎使用”后,“安装文件夹”前添加“许可协议”对话框。
licensefile选择协议,协议的格式为rtf。
6)启动条件视图
为启动安装程序制定最低framework要求。
7)实现安装、卸载过程中的其他额外的操作。比如安装结束后启动程序,卸载程序后同时删除网络下载打安装包等功能。
a.新建一个空的项目InstallCompenent,步骤为:解决方案->右键添加->新建项目->选择"空项目"->
输入名称"InstallCompenent"->确定,完成项目的添加.
b.在InstallCompenent项目中右键->添加->新建项->选择安装程序类->输入名称"Installer",完成installer类的添加.
修改代码为:
<span style="color: blue;">using </span>System; <span style="color: blue;">using </span>System.Collections; <span style="color: blue;">using </span>System.Collections.Generic; <span style="color: blue;">using </span>System.ComponentModel; <span style="color: blue;">using </span>System.Configuration.Install; <span style="color: blue;">using </span>System.Reflection; <span style="color: blue;">using </span>System.IO; <span style="color: blue;">namespace </span>InstallCompenent { [<span style="color: rgb(43, 145, 175);">RunInstaller</span>(<span style="color: blue;">true</span>)] <span style="color: blue;">public partial class </span><span style="color: rgb(43, 145, 175);">UccompanionInstaller </span>: <span style="color: rgb(43, 145, 175);">Installer </span>{ <span style="color: blue;">private const string </span>zipPacket = <span style="color: rgb(163, 21, 21);">"UCCompanionSetup(0918).zip"</span>; <span style="color: gray;">/// <summary> /// </span><span style="color: green;">应用程序入口 </span><span style="color: gray;">/// </summary> </span><span style="color: blue;">public static void </span>Main() { } <span style="color: gray;">/// <summary> /// </span><span style="color: green;">构造函数 </span><span style="color: gray;">/// </summary> </span><span style="color: blue;">public </span>UccompanionInstaller() { InitializeComponent(); } <span style="color: gray;">/// <summary> /// </span><span style="color: green;">重写安装完成后函数 </span><span style="color: gray;">/// </span><span style="color: green;">实现安装完成后自动启动已安装的程序 </span><span style="color: gray;">/// </summary> /// <param name="savedState"></param> </span><span style="color: blue;">protected override void </span>OnAfterInstall(<span style="color: rgb(43, 145, 175);">IDictionary </span>savedState) { <span style="color: blue;">base</span>.OnAfterInstall(savedState); <span style="color: rgb(43, 145, 175);">Assembly </span>asm = <span style="color: rgb(43, 145, 175);">Assembly</span>.GetExecutingAssembly(); <span style="color: blue;">string </span>path = asm.Location.Remove(asm.Location.LastIndexOf(<span style="color: rgb(163, 21, 21);">"\\"</span>)) + <span style="color: rgb(163, 21, 21);">"\\"</span>; System.Diagnostics.<span style="color: rgb(43, 145, 175);">Process</span>.Start(path + <span style="color: rgb(163, 21, 21);">"\\UCCompanion.exe"</span>); } <span style="color: gray;">/// <summary> /// </span><span style="color: green;">重写安装过程方法 </span><span style="color: gray;">/// </summary> /// <param name="stateSaver"></param> </span><span style="color: blue;">public override void </span>Install(<span style="color: rgb(43, 145, 175);">IDictionary </span>stateSaver) { <span style="color: blue;">base</span>.Install(stateSaver); } <span style="color: gray;">/// <summary> /// </span><span style="color: green;">重写安装之前方法 </span><span style="color: gray;">/// </summary> /// <param name="savedState"></param> </span><span style="color: blue;">protected override void </span>OnBeforeInstall(<span style="color: rgb(43, 145, 175);">IDictionary </span>savedState) { <span style="color: blue;">base</span>.OnBeforeInstall(savedState); } <span style="color: gray;">/// <summary> /// </span><span style="color: green;">重写卸载方法 </span><span style="color: gray;">/// </span><span style="color: green;">卸载程序后也删除程序的安装包 </span><span style="color: gray;">/// </summary> /// <param name="savedState"></param> </span><span style="color: blue;">public override void </span>Uninstall(<span style="color: rgb(43, 145, 175);">IDictionary </span>savedState) { <span style="color: blue;">string </span>temp = System.<span style="color: rgb(43, 145, 175);">Environment</span>.GetEnvironmentVariable(<span style="color: rgb(163, 21, 21);">"TEMP"</span>); <span style="color: blue;">string </span>folder = <span style="color: blue;">new </span><span style="color: rgb(43, 145, 175);">DirectoryInfo</span>(temp).FullName; <span style="color: blue;">if </span>(<span style="color: rgb(43, 145, 175);">File</span>.Exists(folder + <span style="color: rgb(163, 21, 21);">@"\setup.exe"</span>)) { <span style="color: rgb(43, 145, 175);">File</span>.Delete(folder + <span style="color: rgb(163, 21, 21);">@"\setup.exe"</span>); } <span style="color: blue;">if </span>(<span style="color: rgb(43, 145, 175);">File</span>.Exists(folder + <span style="color: rgb(163, 21, 21);">@"\setup.msi"</span>)) { <span style="color: rgb(43, 145, 175);">File</span>.Delete(folder + <span style="color: rgb(163, 21, 21);">@"\setup.msi"</span>); } <span style="color: blue;">if </span>(<span style="color: rgb(43, 145, 175);">File</span>.Exists(folder + <span style="color: rgb(163, 21, 21);">@"\"</span>+zipPacket)) { <span style="color: rgb(43, 145, 175);">File</span>.Delete(folder + <span style="color: rgb(163, 21, 21);">@"\"</span>+zipPacket); } <span style="color: blue;">base</span>.Uninstall(savedState); } <span style="color: gray;">/// <summary> /// </span><span style="color: green;">重写回滚方法 </span><span style="color: gray;">/// </summary> /// <param name="savedState"></param> </span><span style="color: blue;">public override void </span>Rollback(<span style="color: rgb(43, 145, 175);">IDictionary </span>savedState) { <span style="color: blue;">base</span>.Rollback(savedState); } } }
c.在安装项目中右键->添加项目输出->选择"项目"->InstallCompenent.完成主输出项目的添加.
d.打开自定义操作编辑器,在安装->右键->添加自定义操作->选择"应用程序文件夹"->选择"主输出来自InstallCompenent",完成添加.
好了,点击“生成解决方案”,即可以生成带有卸载功能的安装程序了。