您的位置:首页 > 编程语言 > C#

c#自动更新+安装程序的制作

2015-06-04 21:41 716 查看
基于remoting程序更新

http://www.cnblogs.com/sdav/archive/2008/11/03/1325727.html

http://www.cnblogs.com/cxy521/articles/1238247.html


c#自动更新+安装程序的制作

分类:C#2011-11-02
10:467789人阅读评论(3)收藏举报

c#stringinstallerexceptionobjectshell

一、自动更新的实现

让客户端实现自动更新,通常做法是在客户端部署一个单独的自动更新程序。主程序启动后,访问服务端,检查配置文件是

否有更新版本,有更新版本就启动更新程序,由更新负责下载更新版本,并更新客户端程序,流程如下:



当流程进行到红色部分的是后就调用更新程序进行更新。

1)版本判断:

客户端和服务端都部署同一个版本文件,客户端登陆时发送验证给服务端判断版本是否一致。

Version.xml代码
<spanstyle="color:blue;"><</span><spanstyle="color:rgb(163,21,21);">iq</span><spanstyle="color:red;">xmlns</span><spanstyle="color:blue;">=</span>"<spanstyle="color:blue;">http://www.dynastech.com/xmtp</span>"<spanstyle="color:red;">from</span><spanstyle="color:blue;">=</span>"<spanstyle="color:blue;">*@domcool.local/updater</span>"<spanstyle="color:red;">to</span><spanstyle="color:blue;">=</span>"<spanstyle="color:blue;">*@domcool.local/updater</span>"<spanstyle="color:red;">type</span><spanstyle="color:blue;">=</span>"<spanstyle="color:blue;">get</span>"
<spanstyle="color:red;">id</span><spanstyle="color:blue;">=</span>"<spanstyle="color:blue;">508f3e88-4bb0-4585-a5c6-cc41ef57fef3</span>"<spanstyle="color:blue;">>
<</span><spanstyle="color:rgb(163,21,21);">query</span><spanstyle="color:red;">xmlns</span><spanstyle="color:blue;">=</span>"<spanstyle="color:blue;">http://www.dynastech.com/xmtp/disco#update</span>"<spanstyle="color:red;">version</span><spanstyle="color:blue;">=</span>"<spanstyle="color:blue;">20090922</span>"<spanstyle="color:red;">lastUpdateTime</span><spanstyle="color:blue;">=</span>"<spanstyle="color:blue;">2009-09-22</span>"
<spanstyle="color:red;">fileUrl</span><spanstyle="color:blue;">=</span>"<spanstyle="color:blue;"><atarget=_blankhref="http://172.0.0.1/UCCompanion/UCCompanionSetup(0922).zip%22%3E"style="color:black;"><spanstyle="color:rgb(20,99,196);">http://172.0.0.1/UCCompanion/UCCompanionSetup(0922).zip</span></a><</span>"<spanstyle="color:blue;">>
</span><spanstyle="color:rgb(163,21,21);">x</span><spanstyle="color:red;">xmlns</span><spanstyle="color:blue;">=</span>"<spanstyle="color:blue;">http://www.dynastech.com/xmtp/item</span>"<spanstyle="color:blue;">>
</</span><spanstyle="color:rgb(163,21,21);">x</span><spanstyle="color:blue;">>
</</span><spanstyle="color:rgb(163,21,21);">query</span><spanstyle="color:blue;">>
</</span><spanstyle="color:rgb(163,21,21);">iq</span><spanstyle="color:blue;">></span>
版本文件主要比较服务端Version.xml文件和客户端Version.xml文件中Version(版本号)是否一致,如果服务端Version属性
大于客户端的Version属性,则通过服务端的fileUrl属性获取新版本的下载地址。供更新程序使用。
2)删除原有更新包
所有客户端更新文件均下载到C:\DocumentsandSettings\当前用户名\LocalSettings\Temp文件夹内,当客户端运行后首先判
断是否有新更新包需要下载,如果没有则判断该临时文件夹内是否有旧有安装文件,如果存在,则删除旧有安装文件。
<spanstyle="color:blue;">privatevoid</span>RemoveOldSetupFile()
{
<spanstyle="color:blue;">try
</span>{
<spanstyle="color:blue;">string</span>temp=System.<spanstyle="color:rgb(43,145,175);">Environment</span>.GetEnvironmentVariable(<spanstyle="color:rgb(163,21,21);">"TEMP"</span>);
<spanstyle="color:blue;">string</span>folder=<spanstyle="color:blue;">new</span><spanstyle="color:rgb(43,145,175);">DirectoryInfo</span>(temp).FullName;
<spanstyle="color:blue;">if</span>(<spanstyle="color:rgb(43,145,175);">File</span>.Exists(folder+<spanstyle="color:rgb(163,21,21);">@"\"</span>+setupName+<spanstyle="color:rgb(163,21,21);">".exe"</span>))
{
<spanstyle="color:rgb(43,145,175);">File</span>.Delete(folder+<spanstyle="color:rgb(163,21,21);">@"\"</span>+setupName+<spanstyle="color:rgb(163,21,21);">".exe"</span>);
}
<spanstyle="color:blue;">if</span>(<spanstyle="color:rgb(43,145,175);">File</span>.Exists(folder+<spanstyle="color:rgb(163,21,21);">@"\"</span>+setupName+<spanstyle="color:rgb(163,21,21);">".msi"</span>))
{
<spanstyle="color:rgb(43,145,175);">File</span>.Delete(folder+<spanstyle="color:rgb(163,21,21);">@"\"</span>+setupName+<spanstyle="color:rgb(163,21,21);">".msi"</span>);
}
}
<spanstyle="color:blue;">catch</span>{}

}


备注:关于获取系统特殊文件夹的方法见博客/article/5919579.html

3)启动下载程序

下载程序和客户端程序是相互独立的,可以通过客户端开启新线程启动下载程序。下载程序在文件下载结束后可以关掉客户端程序,

并开启新线程启动安装程序进行安装。
<spanstyle="color:blue;">privatevoid</span>Update()
{
<spanstyle="color:blue;">if</span>(ShouldUpdate(query.Version,<spanstyle="color:blue;">this</span>.version))
{
<spanstyle="color:rgb(43,145,175);">MessageBox</span>.Show(<spanstyle="color:rgb(163,21,21);">"请更新客户端文件到版本["</span>+query.Version+<spanstyle="color:rgb(163,21,21);">"]"</span>,<spanstyle="color:rgb(163,21,21);">"更新提示"</span>,<spanstyle="color:rgb(43,145,175);">MessageBoxButtons</span>.OK,
<spanstyle="color:rgb(43,145,175);">MessageBoxIcon</span>.Asterisk);
System.Diagnostics.<spanstyle="color:rgb(43,145,175);">Process</span>.Start(<spanstyle="color:rgb(43,145,175);">Application</span>.StartupPath+<spanstyle="color:rgb(163,21,21);">@"\AutoUpdater.exe"</span>,query.FileUrl);
}
<spanstyle="color:blue;">else</span>{RemoveOldSetupFile();}
}

<spanstyle="color:blue;">privatebool</span>ShouldUpdate(<spanstyle="color:blue;">string</span>serverVersion,<spanstyle="color:blue;">string</span>localVersion)
{
<spanstyle="color:blue;">if</span>(!<spanstyle="color:blue;">string</span>.IsNullOrEmpty(serverVersion)&&!<spanstyle="color:blue;">string</span>.IsNullOrEmpty(localVersion))
{
<spanstyle="color:blue;">return</span>serverVersion.CompareTo(localVersion)>0;
}
<spanstyle="color:blue;">returntrue</span>;
}
调用AutoUpdater.exe文件时需要传入文件下载地址。
System.Diagnostics.<spanstyle="color:rgb(43,145,175);">Process</span>.Start(<spanstyle="color:rgb(43,145,175);">Application</span>.StartupPath+<spanstyle="color:rgb(163,21,21);">@"\AutoUpdater.exe"</span>,query.FileUrl);
4)下载程序代码
下载程序界面
<atarget=_blankhref="http://images.cnblogs.com/cnblogs_com/thornfield_he/WindowsLiveWriter/8645c7345ec9_9E9A/image_20.png"style="color:black;"><imgtitle="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>
<spanstyle="color:blue;">using</span>System;
<spanstyle="color:blue;">using</span>System.Collections.Generic;
<spanstyle="color:blue;">using</span>System.ComponentModel;
<spanstyle="color:blue;">using</span>System.Data;
<spanstyle="color:blue;">using</span>System.Drawing;
<spanstyle="color:blue;">using</span>System.Text;
<spanstyle="color:blue;">using</span>System.Windows.Forms;
<spanstyle="color:blue;">using</span>System.Net;
<spanstyle="color:blue;">using</span>System.IO;
<spanstyle="color:blue;">using</span>System.Threading;
<spanstyle="color:blue;">using</span>System.Diagnostics;

<spanstyle="color:blue;">namespace</span>AutoUpdater
{
<spanstyle="color:blue;">publicpartialclass</span><spanstyle="color:rgb(43,145,175);">MainForm</span>:<spanstyle="color:rgb(43,145,175);">Form
</span>{
<spanstyle="color:blue;">private</span><spanstyle="color:rgb(43,145,175);">WebClient</span>client;
<spanstyle="color:blue;">privatestring</span>URl;
<spanstyle="color:blue;">privatestring</span>fileName;
<spanstyle="color:blue;">privatestring</span>path;
<spanstyle="color:blue;">privateconststring</span>applicationFile=<spanstyle="color:rgb(163,21,21);">"Setup"</span>;

<spanstyle="color:blue;">public</span>MainForm(<spanstyle="color:blue;">string</span>url)
{
InitializeComponent();

<spanstyle="color:blue;">this</span>.URl=url;
client=<spanstyle="color:blue;">new</span><spanstyle="color:rgb(43,145,175);">WebClient</span>();
client.DownloadProgressChanged+=<spanstyle="color:blue;">new</span><spanstyle="color:rgb(43,145,175);">DownloadProgressChangedEventHandler</span>(client_DownloadProgressChanged);
client.DownloadFileCompleted+=<spanstyle="color:blue;">new</span><spanstyle="color:rgb(43,145,175);">AsyncCompletedEventHandler</span>(client_DownloadFileCompleted);
client.Proxy=<spanstyle="color:rgb(43,145,175);">WebRequest</span>.DefaultWebProxy;
client.Proxy.Credentials=<spanstyle="color:blue;">new</span><spanstyle="color:rgb(43,145,175);">NetworkCredential</span>();

<spanstyle="color:blue;">this</span>.Hide();
<spanstyle="color:green;">//Threadthread=newThread(UpdateFile);
//Thread.Sleep(15000);
//thread.Start();
</span>UpdateFile();
}

<spanstyle="color:blue;">public</span>MainForm()
{
InitializeComponent();
}

<spanstyle="color:gray;">///<summary>
///</span><spanstyle="color:green;">下载完成调用
</span><spanstyle="color:gray;">///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
</span><spanstyle="color:blue;">void</span>client_DownloadFileCompleted(<spanstyle="color:blue;">object</span>sender,<spanstyle="color:rgb(43,145,175);">AsyncCompletedEventArgs</span>e)
{
label1.Text=<spanstyle="color:rgb(163,21,21);">"文件接收完成"</span>;
UnZip();
RunUpdate();
}

<spanstyle="color:gray;">///<summary>
///</span><spanstyle="color:green;">下载进度条
</span><spanstyle="color:gray;">///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
</span><spanstyle="color:blue;">void</span>client_DownloadProgressChanged(<spanstyle="color:blue;">object</span>sender,<spanstyle="color:rgb(43,145,175);">DownloadProgressChangedEventArgs</span>e)
{
<spanstyle="color:blue;">this</span>.progressBar1.Value=e.ProgressPercentage;
}

<spanstyle="color:gray;">///<summary>
///</span><spanstyle="color:green;">开始下载
</span><spanstyle="color:gray;">///</summary>
</span><spanstyle="color:blue;">privatevoid</span>StartDownload()
{
fileName=URl.Substring(URl.LastIndexOf(<spanstyle="color:rgb(163,21,21);">"/"</span>)+1,URl.Length-URl.LastIndexOf(<spanstyle="color:rgb(163,21,21);">"/"</span>)-1);
path=GetTempFolder();

<spanstyle="color:blue;">try
</span>{
<spanstyle="color:rgb(43,145,175);">WebRequest</span>myre=<spanstyle="color:rgb(43,145,175);">WebRequest</span>.Create(URl);
}
<spanstyle="color:blue;">catch</span>(<spanstyle="color:rgb(43,145,175);">Exception</span>ex)
{
<spanstyle="color:rgb(43,145,175);">MessageBox</span>.Show(ex.Message,<spanstyle="color:rgb(163,21,21);">"Error"</span>);
}

<spanstyle="color:blue;">try
</span>{
label1.Text=<spanstyle="color:rgb(163,21,21);">"开始下载文件..."</span>;
client.DownloadFileAsync(<spanstyle="color:blue;">new</span><spanstyle="color:rgb(43,145,175);">Uri</span>(URl),path+<spanstyle="color:rgb(163,21,21);">@"\"</span>+fileName);

}
<spanstyle="color:blue;">catch</span>(<spanstyle="color:rgb(43,145,175);">WebException</span>exp)
{
label1.Text=exp.Message;
}
}

<spanstyle="color:gray;">///<summary>
///</span><spanstyle="color:green;">解压压缩包,格式必须是*.zip,否则不能解压
</span><spanstyle="color:gray;">///</span><spanstyle="color:green;">因为是调用Windows内部api进行解压,只能够识别zip压缩包
</span><spanstyle="color:gray;">///</span><spanstyle="color:green;">必须添加C:\WINDOWS\system32\shell32.dll的引用
</span><spanstyle="color:gray;">///</summary>
</span><spanstyle="color:blue;">privatevoid</span>UnZip()
{
<spanstyle="color:blue;">try
</span>{
Shell32.<spanstyle="color:rgb(43,145,175);">ShellClass</span>sc=<spanstyle="color:blue;">new</span>Shell32.<spanstyle="color:rgb(43,145,175);">ShellClass</span>();
Shell32.<spanstyle="color:rgb(43,145,175);">Folder</span>SrcFolder=sc.NameSpace(<spanstyle="color:blue;">this</span>.path+<spanstyle="color:rgb(163,21,21);">@"\"</span>+<spanstyle="color:blue;">this</span>.fileName);
Shell32.<spanstyle="color:rgb(43,145,175);">Folder</span>DestFolder=sc.NameSpace(<spanstyle="color:blue;">this</span>.path);
Shell32.<spanstyle="color:rgb(43,145,175);">FolderItems</span>items=SrcFolder.Items();
DestFolder.CopyHere(items,20);
}
<spanstyle="color:blue;">catch</span>(<spanstyle="color:rgb(43,145,175);">Exception</span>ex)
{
<spanstyle="color:rgb(43,145,175);">MessageBox</span>.Show(ex.Message);
}
}

<spanstyle="color:gray;">///<summary>
///</span><spanstyle="color:green;">获取下载文件夹地址及解压文件存放地址
</span><spanstyle="color:gray;">///</span><spanstyle="color:green;">此地址默认为C:\DocumentsandSettings\当前用户名\LocalSettings\Temp文件夹
</span><spanstyle="color:gray;">///</summary>
///<returns></returns>
</span><spanstyle="color:blue;">privatestring</span>GetTempFolder()
{
<spanstyle="color:blue;">string</span>folder=System.<spanstyle="color:rgb(43,145,175);">Environment</span>.GetEnvironmentVariable(<spanstyle="color:rgb(163,21,21);">"TEMP"</span>);
<spanstyle="color:blue;">returnnew</span><spanstyle="color:rgb(43,145,175);">DirectoryInfo</span>(folder).FullName;
}

<spanstyle="color:gray;">///<summary>
///</span><spanstyle="color:green;">开始下载文件
</span><spanstyle="color:gray;">///</summary>
</span><spanstyle="color:blue;">privatevoid</span>UpdateFile()
{
<spanstyle="color:blue;">this</span>.Hide();
<spanstyle="color:green;">//如果临时文件夹存在setup安装文件,就直接调用安装文件
</span><spanstyle="color:blue;">if</span>(<spanstyle="color:rgb(43,145,175);">File</span>.Exists(GetTempFolder()+<spanstyle="color:rgb(163,21,21);">@"\"</span>+applicationFile+<spanstyle="color:rgb(163,21,21);">".exe"</span>)&&<spanstyle="color:rgb(43,145,175);">File</span>.Exists(GetTempFolder()+
<spanstyle="color:rgb(163,21,21);">@"\"</span>+applicationFile+<spanstyle="color:rgb(163,21,21);">".msi"</span>))
{
label1.Text=<spanstyle="color:rgb(163,21,21);">"开始下载文件..."</span>;
<spanstyle="color:blue;">this</span>.progressBar1.Value=<spanstyle="color:blue;">this</span>.progressBar1.Maximum;
label1.Text=<spanstyle="color:rgb(163,21,21);">"文件接收完成"</span>;
RunUpdate();
}
<spanstyle="color:green;">//如果临时文件夹不存在setup安装文件,就从网络下载
</span><spanstyle="color:blue;">else
</span>{
RemoveSetupFile();
StartDownload();
}
}

<spanstyle="color:gray;">///<summary>
///</span><spanstyle="color:green;">清除旧有已下载的安装文件
</span><spanstyle="color:gray;">///</summary>
</span><spanstyle="color:blue;">privatestaticvoid</span>RemoveSetupFile()
{
<spanstyle="color:blue;">try
</span>{
<spanstyle="color:blue;">string</span>temp=System.<spanstyle="color:rgb(43,145,175);">Environment</span>.GetEnvironmentVariable(<spanstyle="color:rgb(163,21,21);">"TEMP"</span>);
<spanstyle="color:blue;">string</span>folder=<spanstyle="color:blue;">new</span><spanstyle="color:rgb(43,145,175);">DirectoryInfo</span>(temp).FullName;
<spanstyle="color:blue;">if</span>(<spanstyle="color:rgb(43,145,175);">File</span>.Exists(folder+<spanstyle="color:rgb(163,21,21);">@"\"</span>+applicationFile+<spanstyle="color:rgb(163,21,21);">".exe"</span>))
{
<spanstyle="color:rgb(43,145,175);">File</span>.Delete(folder+<spanstyle="color:rgb(163,21,21);">@"\"</span>+applicationFile+<spanstyle="color:rgb(163,21,21);">".exe"</span>);
}
<spanstyle="color:blue;">if</span>(<spanstyle="color:rgb(43,145,175);">File</span>.Exists(folder+<spanstyle="color:rgb(163,21,21);">@"\"</span>+applicationFile+<spanstyle="color:rgb(163,21,21);">".msi"</span>))
{
<spanstyle="color:rgb(43,145,175);">File</span>.Delete(folder+<spanstyle="color:rgb(163,21,21);">@"\"</span>+applicationFile+<spanstyle="color:rgb(163,21,21);">".msi"</span>);
}
}
<spanstyle="color:blue;">catch</span>{}
}

<spanstyle="color:gray;">///<summary>
///</span><spanstyle="color:green;">下载完毕,开始执行更新程序
</span><spanstyle="color:gray;">///</summary>
</span><spanstyle="color:blue;">privatevoid</span>RunUpdate()
{
<spanstyle="color:blue;">try
</span>{
<spanstyle="color:blue;">foreach</span>(<spanstyle="color:rgb(43,145,175);">Process</span>p<spanstyle="color:blue;">in</span><spanstyle="color:rgb(43,145,175);">Process</span>.GetProcesses())
{
<spanstyle="color:blue;">if</span>(p.ProcessName.ToLower().StartsWith(<spanstyle="color:rgb(163,21,21);">"uccompanion"</span>))
{
<spanstyle="color:blue;">if</span>(<spanstyle="color:rgb(43,145,175);">MessageBox</span>.Show(<spanstyle="color:rgb(163,21,21);">"UCCompanion正在运行,是否关闭当前程序安装更新?"</span>,<spanstyle="color:rgb(163,21,21);">"安装UCCompanion"</span>,
<spanstyle="color:rgb(43,145,175);">MessageBoxButtons</span>.YesNo,<spanstyle="color:rgb(43,145,175);">MessageBoxIcon</span>.Question)==<spanstyle="color:rgb(43,145,175);">DialogResult</span>.Yes)
{
p.Kill();
<spanstyle="color:rgb(43,145,175);">Process</span>.Start(GetTempFolder()+<spanstyle="color:rgb(163,21,21);">@"\"</span>+applicationFile+<spanstyle="color:rgb(163,21,21);">".exe"</span>);
}
<spanstyle="color:blue;">else
</span>{
<spanstyle="color:rgb(43,145,175);">MessageBox</span>.Show(<spanstyle="color:rgb(163,21,21);">"UCCompanion下载完成,将在下次启动时提醒更新!"</span>);
}
}
}

}
<spanstyle="color:blue;">catch</span>(<spanstyle="color:rgb(43,145,175);">Exception</span>ex)
{
<spanstyle="color:rgb(43,145,175);">MessageBox</span>.Show(ex.Message);
}
<spanstyle="color:blue;">finally
</span>{
<spanstyle="color:blue;">this</span>.Close();
}
}

<spanstyle="color:gray;">///<summary>
///</span><spanstyle="color:green;">重载WindProc判断点击关闭按钮(X)时,隐藏程序界面
</span><spanstyle="color:gray;">///</summary>
///<paramname="msg"></param>
</span><spanstyle="color:blue;">protectedoverridevoid</span>WndProc(<spanstyle="color:blue;">ref</span><spanstyle="color:rgb(43,145,175);">Message</span>msg)
{
<spanstyle="color:blue;">constint</span>WM_SYSCOMMAND=0x0112;
<spanstyle="color:blue;">constint</span>SC_CLOSE=0xF060;

<spanstyle="color:blue;">if</span>(msg.Msg==WM_SYSCOMMAND&&((<spanstyle="color:blue;">int</span>)msg.WParam==SC_CLOSE))
{
<spanstyle="color:blue;">this</span>.Hide();
<spanstyle="color:blue;">return</span>;
}
<spanstyle="color:blue;">base</span>.WndProc(<spanstyle="color:blue;">ref</span>msg);
}

<spanstyle="color:gray;">///<summary>
///</span><spanstyle="color:green;">双击图标弹出界面
</span><spanstyle="color:gray;">///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
</span><spanstyle="color:blue;">privatevoid</span>icon_notify_MouseDoubleClick(<spanstyle="color:blue;">object</span>sender,<spanstyle="color:rgb(43,145,175);">MouseEventArgs</span>e)
{
<spanstyle="color:blue;">this</span>.Show();
<spanstyle="color:blue;">this</span>.WindowState=<spanstyle="color:rgb(43,145,175);">FormWindowState</span>.Normal;
}

<spanstyle="color:gray;">///<summary>
///
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
</span><spanstyle="color:blue;">privatevoid</span>MainForm_SizeChanged(<spanstyle="color:blue;">object</span>sender,<spanstyle="color:rgb(43,145,175);">EventArgs</span>e)
{
<spanstyle="color:blue;">if</span>(<spanstyle="color:blue;">this</span>.WindowState==<spanstyle="color:rgb(43,145,175);">FormWindowState</span>.Minimized)
{
<spanstyle="color:blue;">this</span>.Hide();
}
}

<spanstyle="color:blue;">privatevoid</span>MainForm_Load(<spanstyle="color:blue;">object</span>sender,<spanstyle="color:rgb(43,145,175);">EventArgs</span>e)
{
<spanstyle="color:blue;">this</span>.Hide();
}

}

<spanstyle="color:blue;">staticclass</span><spanstyle="color:rgb(43,145,175);">Program
</span>{
<spanstyle="color:gray;">///<summary>
///</span><spanstyle="color:green;">启动,接收传入网址作为参数
</span><spanstyle="color:gray;">///</summary>
///<paramname="agr"></param>
</span>[<spanstyle="color:rgb(43,145,175);">STAThread</span>]
<spanstyle="color:blue;">staticvoid</span>Main(<spanstyle="color:blue;">string</span>[]agr)
{
<spanstyle="color:blue;">if</span>(agr.Length==1&&agr[0].StartsWith(<spanstyle="color:rgb(163,21,21);">@"http://"</span>))
{
<spanstyle="color:rgb(43,145,175);">MainForm</span>form=<spanstyle="color:blue;">new</span><spanstyle="color:rgb(43,145,175);">MainForm</span>(agr[0]);
<spanstyle="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:\programmfile\制造商名称\安装解决方案名称";





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类的添加.

修改代码为:
<spanstyle="color:blue;">using</span>System;
<spanstyle="color:blue;">using</span>System.Collections;
<spanstyle="color:blue;">using</span>System.Collections.Generic;
<spanstyle="color:blue;">using</span>System.ComponentModel;
<spanstyle="color:blue;">using</span>System.Configuration.Install;
<spanstyle="color:blue;">using</span>System.Reflection;
<spanstyle="color:blue;">using</span>System.IO;

<spanstyle="color:blue;">namespace</span>InstallCompenent
{
[<spanstyle="color:rgb(43,145,175);">RunInstaller</span>(<spanstyle="color:blue;">true</span>)]
<spanstyle="color:blue;">publicpartialclass</span><spanstyle="color:rgb(43,145,175);">UccompanionInstaller</span>:<spanstyle="color:rgb(43,145,175);">Installer
</span>{
<spanstyle="color:blue;">privateconststring</span>zipPacket=<spanstyle="color:rgb(163,21,21);">"UCCompanionSetup(0918).zip"</span>;
<spanstyle="color:gray;">///<summary>
///</span><spanstyle="color:green;">应用程序入口
</span><spanstyle="color:gray;">///</summary>
</span><spanstyle="color:blue;">publicstaticvoid</span>Main()
{
}
<spanstyle="color:gray;">///<summary>
///</span><spanstyle="color:green;">构造函数
</span><spanstyle="color:gray;">///</summary>
</span><spanstyle="color:blue;">public</span>UccompanionInstaller()
{
InitializeComponent();
}
<spanstyle="color:gray;">///<summary>
///</span><spanstyle="color:green;">重写安装完成后函数
</span><spanstyle="color:gray;">///</span><spanstyle="color:green;">实现安装完成后自动启动已安装的程序
</span><spanstyle="color:gray;">///</summary>
///<paramname="savedState"></param>
</span><spanstyle="color:blue;">protectedoverridevoid</span>OnAfterInstall(<spanstyle="color:rgb(43,145,175);">IDictionary</span>savedState)
{
<spanstyle="color:blue;">base</span>.OnAfterInstall(savedState);
<spanstyle="color:rgb(43,145,175);">Assembly</span>asm=<spanstyle="color:rgb(43,145,175);">Assembly</span>.GetExecutingAssembly();
<spanstyle="color:blue;">string</span>path=asm.Location.Remove(asm.Location.LastIndexOf(<spanstyle="color:rgb(163,21,21);">"\\"</span>))+<spanstyle="color:rgb(163,21,21);">"\\"</span>;
System.Diagnostics.<spanstyle="color:rgb(43,145,175);">Process</span>.Start(path+<spanstyle="color:rgb(163,21,21);">"\\UCCompanion.exe"</span>);
}
<spanstyle="color:gray;">///<summary>
///</span><spanstyle="color:green;">重写安装过程方法
</span><spanstyle="color:gray;">///</summary>
///<paramname="stateSaver"></param>
</span><spanstyle="color:blue;">publicoverridevoid</span>Install(<spanstyle="color:rgb(43,145,175);">IDictionary</span>stateSaver)
{
<spanstyle="color:blue;">base</span>.Install(stateSaver);
}
<spanstyle="color:gray;">///<summary>
///</span><spanstyle="color:green;">重写安装之前方法
</span><spanstyle="color:gray;">///</summary>
///<paramname="savedState"></param>
</span><spanstyle="color:blue;">protectedoverridevoid</span>OnBeforeInstall(<spanstyle="color:rgb(43,145,175);">IDictionary</span>savedState)
{
<spanstyle="color:blue;">base</span>.OnBeforeInstall(savedState);
}
<spanstyle="color:gray;">///<summary>
///</span><spanstyle="color:green;">重写卸载方法
</span><spanstyle="color:gray;">///</span><spanstyle="color:green;">卸载程序后也删除程序的安装包
</span><spanstyle="color:gray;">///</summary>
///<paramname="savedState"></param>
</span><spanstyle="color:blue;">publicoverridevoid</span>Uninstall(<spanstyle="color:rgb(43,145,175);">IDictionary</span>savedState)
{
<spanstyle="color:blue;">string</span>temp=System.<spanstyle="color:rgb(43,145,175);">Environment</span>.GetEnvironmentVariable(<spanstyle="color:rgb(163,21,21);">"TEMP"</span>);
<spanstyle="color:blue;">string</span>folder=<spanstyle="color:blue;">new</span><spanstyle="color:rgb(43,145,175);">DirectoryInfo</span>(temp).FullName;
<spanstyle="color:blue;">if</span>(<spanstyle="color:rgb(43,145,175);">File</span>.Exists(folder+<spanstyle="color:rgb(163,21,21);">@"\setup.exe"</span>))
{
<spanstyle="color:rgb(43,145,175);">File</span>.Delete(folder+<spanstyle="color:rgb(163,21,21);">@"\setup.exe"</span>);
}
<spanstyle="color:blue;">if</span>(<spanstyle="color:rgb(43,145,175);">File</span>.Exists(folder+<spanstyle="color:rgb(163,21,21);">@"\setup.msi"</span>))
{
<spanstyle="color:rgb(43,145,175);">File</span>.Delete(folder+<spanstyle="color:rgb(163,21,21);">@"\setup.msi"</span>);
}
<spanstyle="color:blue;">if</span>(<spanstyle="color:rgb(43,145,175);">File</span>.Exists(folder+<spanstyle="color:rgb(163,21,21);">@"\"</span>+zipPacket))
{
<spanstyle="color:rgb(43,145,175);">File</span>.Delete(folder+<spanstyle="color:rgb(163,21,21);">@"\"</span>+zipPacket);
}
<spanstyle="color:blue;">base</span>.Uninstall(savedState);
}
<spanstyle="color:gray;">///<summary>
///</span><spanstyle="color:green;">重写回滚方法
</span><spanstyle="color:gray;">///</summary>
///<paramname="savedState"></param>
</span><spanstyle="color:blue;">publicoverridevoid</span>Rollback(<spanstyle="color:rgb(43,145,175);">IDictionary</span>savedState)
{
<spanstyle="color:blue;">base</span>.Rollback(savedState);
}
}
}


c.在安装项目中右键->添加项目输出->选择"项目"->InstallCompenent.完成主输出项目的添加.

d.打开自定义操作编辑器,在安装->右键->添加自定义操作->选择"应用程序文件夹"->选择"主输出来自InstallCompenent",完成添加.

好了,点击“生成解决方案”,即可以生成带有卸载功能的安装程序了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: