基于C/S模式的程序更新下载

思路:

①检查是否需要更新(通过数据库获取最新版本号和本地版本号进行比对(本地版本号可存在txt文件中,需要进行一定程度上的加密和解密操作))

②从指定目录下载最新版本的程序覆盖本地文件(下载的文件时压缩文件)

③将压缩文件进行程序内解压缩(有可能客户电脑未安装压缩软件,所以只能通过程序本身来进行解压)

④解压完成后调用主程序

实现:

下载:WebClient

 1 if (webClient.IsBusy)//是否存在正在进行中的Web请求
 2             {
 3                 webClient.CancelAsync();
 4             }
 5             //为webClient添加事件
 6             webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);
 7             webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(webClient_DownloadFileCompleted);
 8             //开始下载
 9             string path = exePath + @"\Update.RAR";
10             webClient.DownloadFileAsync(new Uri(this.txtFilePath.Text), path);
 1  private void webClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
 2         {
 3 
 4             this.progressBar1.Value = e.ProgressPercentage;
 5             this.lbl_pro.Text = e.ProgressPercentage.ToString() + "%";
 6             this.lbl_detail.Text = string.Format("Downloading....,{0}/{1}(byte)"
 7                                 , e.BytesReceived
 8                                 , e.TotalBytesToReceive);
 9             if (e.TotalBytesToReceive == e.BytesReceived)
10             {
11                 this.lbl_detail.Text = ("DownLoad success.Update now, wait a second!");
12             }
13         }
 1  private void webClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
 2         {
 3             if (e.Cancelled)
 4             {
 5                 DevExpress.XtraEditors.XtraMessageBox.Show("Download is canceled!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
 6                 return;
 7             }
 8             else
 9             {
10 
11                 if (this.lbl_detail.Text == ("DownLoad success.Update now, wait a second!"))
12                 {
13                     Thread t = new Thread(//实际上进行更新的方法);
14                     CheckForIllegalCrossThreadCalls = false;
15                     t.Start();
16                 }
17 
18             }
19         }
原文地址:https://www.cnblogs.com/JoeyZJ/p/5653252.html