C# Winform自动更新程序实例详解
本文实例为大家分享了C#Winform自动更新程序,供大家参考,具体内容如下
第一步:检查更新
检查更新其实无非就是去比较更新包的版本和本地软件版本,如果高则更新、低则不更新。怎么获取版本号方法很多,本案例是获取软件的配置文件。
privateboolCheckUpdate()
{
boolresult=false;
try
{
stringCfg=TxtRead(exePath"\\Config.txt");
ConfigLocal=JsonConvert.DeserializeObject(Cfg);
CheckUpdateURL=ConfigLocal.AutoUpdateURL;
Cfg=TxtRead(CheckUpdateURL"\\Config.txt");
ConfigRemote=JsonConvert.DeserializeObject(Cfg);
VersionR=ConfigRemote.Version;
VersionL=ConfigLocal.Version;
intVersionRemote=int.Parse(ConfigRemote.Version.Replace(".",""));
intVersionLocal=int.Parse(ConfigLocal.Version.Replace(".",""));
result=VersionRemote>VersionLocal;
}
catch{}
returnresult;
}
第二步:下载更新包
因为C/S的软件更新是面对所有用户,S端除了给C端提供基本的服务外,还可以给C端提供更新包。而这个S端可以是网络上的一个固定地址,也可以是局域网内一个公共盘。那下载更新包无非就是去访问服务端的文件,然后Copy下来或下载下来。下面给出访问网络和访问局域网两个案例:
A、访问远程网络地址这里采用的是WebClient
publicvoidDownLoadFile()
{
if(!Directory.Exists(UpdateFiles))
{
Directory.CreateDirectory(UpdateFiles);
}
using(WebClientwebClient=newWebClient())
{
try
{
webClient.DownloadFileCompleted=newAsyncCompletedEventHandler(client_DownloadFileCompleted);
webClient.DownloadProgressChanged=newDownloadProgressChangedEventHandler(client_DownloadProgressChanged);
webClient.DownloadFileAsync(newUri(CheckUpdateURL"\\UpdateFile.rar"),UpdateFiles"\\UpdateFile.rar");
}
catch(WebExceptionex)
{
MessageBox.Show(ex.Message,"系统提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
}
这里面应用到两个方法,DownloadProgressChanged,监听异步下载的进度;DownloadFileCompleted,监听完成异步文件下载;
privatevoidclient_DownloadProgressChanged(objectsender,DownloadProgressChangedEventArgse)
{
this.progressBarUpdate.Minimum=0;
this.progressBarUpdate.Maximum=(int)e.TotalBytesToReceive;
this.progressBarUpdate.Value=(int)e.BytesReceived;
this.lblPercent.Text=e.ProgressPercentage"%";
}
privatevoidclient_DownloadFileCompleted(objectsender,AsyncCompletedEventArgse)
{
if(e.Error!=null)
{
MessageBox.Show(e.Error.Message,"系统提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
else
{
this.lblMessage.Text="下载完成";
//复制更新文件替换旧文件
DirectoryInfoTheFolder=newDirectoryInfo(UpdateFiles);
foreach(FileInfoNextFileinTheFolder.GetFiles())
{
File.Copy(NextFile.FullName,Application.StartupPathNextFile.Name,true);
}
}
}
B、访问服务端公共盘,直接采用File.Copy
publicvoidGetRemoteFile()
{
try
{
DirectoryInfoTheFolder=newDirectoryInfo(CheckUpdateURL);
FileInfo[]FileList=TheFolder.GetFiles();
this.progressBarUpdate.Minimum=0;
this.progressBarUpdate.Maximum=FileList.Length;
foreach(FileInfoNextFileinFileList)
{
if(NextFile.Name!="Config.txt")
{
File.Copy(NextFile.FullName,exePath"\\"NextFile.Name,true);
}
this.lblMessage.Text="更新"NextFile.Name;
this.progressBarUpdate.Value=1;
this.lblPercent.Text="更新进度..."(this.progressBarUpdate.Value/FileList.Length)*100"%";
}
this.lblMessage.Text="更新完成";
//更改本地版本号为最新版本号
ConfigLocal.Version=VersionR;
stringcfgs=JsonConvert.SerializeObject(ConfigLocal);
TxtWrite(Application.StartupPath"\\Config.txt",cfgs);
}
catch(Exceptionex)
{
MessageBox.Show(ex.Message,"系统提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
第三步:替换本地文件
这一步或许在第二步中已经实现了,如果你采用的是File.Copy。替换也就是复制粘贴的问题。采用WebClient下载了zip包,那还需解压一下压缩包然后再File.Copy。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。