C# Winform 自动更新程序实例详解
本文实例为大家分享了C# Winform 自动更新程序,供大家参考,具体内容如下
第一步:检查更新
检查更新其实无非就是去比较更新包的版本和本地软件版本,如果高则更新、低则不更新。怎么获取版本号方法很多,本案例是获取软件的配置文件。
private bool CheckUpdate() { bool result = false; try { string Cfg = TxtRead(exePath "\\Config.txt"); ConfigLocal = JsonConvert.DeserializeObject<DTO_Config>(Cfg); CheckUpdateURL = ConfigLocal.AutoUpdateURL; Cfg = TxtRead(CheckUpdateURL "\\Config.txt"); ConfigRemote = JsonConvert.DeserializeObject<DTO_Config>(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端可以是网络上的一个固定地址,也可以是局域网内一个公共盘。那下载更新包无非就是去访问服务端的文件,然后Copy下来或下载下来。下面给出访问网络和访问局域网两个案例:
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); } }
第三步:替换本地文件
这一步或许在第二步中已经实现了,如果你采用的是File.Copy。替换也就是复制粘贴的问题。采用WebClient下载了zip包,那还需解压一下压缩包然后再File.Copy。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
栏 目:C#教程
本文地址:https://www.xiuzhanwang.com/a1/C_jiaocheng/5358.html
您可能感兴趣的文章
- 01-10WinForm限制窗体不能移到屏幕外的方法
- 01-10WinForm绘制圆角的方法
- 01-10WinForm实现仿视频播放器左下角滚动新闻效果的方法
- 01-10C#实现Winform中打开网页页面的方法
- 01-10winform实现创建最前端窗体的方法
- 01-10WinForm实现自定义右下角提示效果的方法
- 01-10.net2.0+ Winform项目实现弹出容器层
- 01-10winform 实现控制输入法
- 01-10WinForm实现程序一段时间不运行自动关闭的方法
- 01-10Winform消除button按下出现的虚线简单实现方法
阅读排行
本栏相关
- 01-10C#通过反射获取当前工程中所有窗体并
- 01-10关于ASP网页无法打开的解决方案
- 01-10WinForm限制窗体不能移到屏幕外的方法
- 01-10WinForm绘制圆角的方法
- 01-10C#实现txt定位指定行完整实例
- 01-10WinForm实现仿视频播放器左下角滚动新
- 01-10C#停止线程的方法
- 01-10C#实现清空回收站的方法
- 01-10C#通过重写Panel改变边框颜色与宽度的
- 01-10C#实现读取注册表监控当前操作系统已
随机阅读
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 01-10C#中split用法实例总结
- 04-02jquery与jsp,用jquery
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 01-11ajax实现页面的局部加载
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 01-10delphi制作wav文件的方法
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 01-10SublimeText编译C开发环境设置