网站内容自动更新(C#访问网络和访问局域网两个程序实例分享实例)
优采云 发布时间: 2021-12-15 14:11网站内容自动更新(C#访问网络和访问局域网两个程序实例分享实例)
本文文章主要详细介绍C#Winform自动更新程序示例。文章中的示例代码很详细,有一定的参考价值。有兴趣的朋友可以参考一下。
本文示例分享C#Winform自动更新程序,供大家参考。具体内容如下
第 1 步:检查更新
检查更新其实无非是将更新包的版本与本地软件版本进行比较。有多种方法可以获取版本号。本例是获取软件的配置文件。
private bool CheckUpdate() { bool result = false; try { string Cfg = 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; int VersionRemote = int.Parse(ConfigRemote.Version.Replace(".", "")); int VersionLocal = int.Parse(ConfigLocal.Version.Replace(".", "")); result = VersionRemote > VersionLocal; } catch { } return result; }
第二步:下载更新包
由于C/S软件更新是针对所有用户的,所以S端除了向C端提供基础服务外,还可以向C端提供更新包。S端可以是网络上的固定地址,也可以是局域网内的公共磁盘。下载更新包无非就是访问服务器上的文件,然后复制或者下载。下面给出接入网络和接入局域网两种情况:
A、访问远程网络地址,这里使用WebClient
public void DownLoadFile() { if (!Directory.Exists(UpdateFiles)) { Directory.CreateDirectory(UpdateFiles); } using (WebClient webClient = new WebClient()) { try { webClient.DownloadFileCompleted = new AsyncCompletedEventHandler(client_DownloadFileCompleted); webClient.DownloadProgressChanged = new DownloadProgressChangedEventHandler(client_DownloadProgressChanged); webClient.DownloadFileAsync(new Uri(CheckUpdateURL "\\UpdateFile.rar"), UpdateFiles "\\UpdateFile.rar"); } catch (WebException ex) { MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
这里应用了两种方法,DownloadProgressChanged,用于监控异步下载的进度;DownloadFileCompleted,监控异步文件下载完成情况;
private void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) { this.progressBarUpdate.Minimum = 0; this.progressBarUpdate.Maximum = (int)e.TotalBytesToReceive; this.progressBarUpdate.Value = (int)e.BytesReceived; this.lblPercent.Text = e.ProgressPercentage "%"; }
private void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) { if (e.Error != null) { MessageBox.Show(e.Error.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { this.lblMessage.Text = "下载完成"; //复制更新文件替换旧文件 DirectoryInfo TheFolder = new DirectoryInfo(UpdateFiles); foreach (FileInfo NextFile in TheFolder.GetFiles()) { File.Copy(NextFile.FullName, Application.StartupPath NextFile.Name, true); } } }
B、访问服务器端的公盘,直接使用File.Copy
public void GetRemoteFile() { try { DirectoryInfo TheFolder = new DirectoryInfo(CheckUpdateURL); FileInfo[] FileList = TheFolder.GetFiles(); this.progressBarUpdate.Minimum = 0; this.progressBarUpdate.Maximum = FileList.Length; foreach (FileInfo NextFile in FileList) { 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; string cfgs = JsonConvert.SerializeObject(ConfigLocal); TxtWrite(Application.StartupPath "\\Config.txt", cfgs); } catch (Exception ex) { MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
第 3 步:替换本地文件
如果您使用的是 File.Copy,则此步骤可能已在第二步中实现。替换是复制和粘贴的问题。使用WebClient下载zip包,然后需要解压压缩包,然后File.Copy。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持html中文网。
以上就是C#Winform自动更新程序示例详解的详细内容。更多详情请关注其他相关html中文网站文章!