- 36
- 0
- 约2.7万字
- 约 16页
- 2017-06-04 发布于河南
- 举报
在WinForm应用程序中实现自动升级功能
在WinForm应用程序中实现自动升级功能
首先,要确定程序应去哪里下载需要升级的文件。我选择了到指定的网站上去下载,这样比较简单,也通用一些。在网站上,需放置一个当前描述最新文件列表的文件,我估且叫它服务器配置文件。这个文件保存了当前最新文件的版本号(lastver),大小(size),下载地址(url),本地文件的保存路径(path),还有更新后,程序是否需要重新启动(needRestart)。updateService.xml
?xml version=1.0 encoding=utf-8 ?
updateFiles
file path=1.jpg url=/1.jpg lastver= size=100 needRestart=true /
file path=2.jpg url=/2.jpg lastver= size=100 needRestart=true /
file path=3.jpg url=/3.jpg lastver= size=100 needRestart=true /
file path=4.jpg url=/4.jpg lastver= size=100 needRestart=true /
file path=5.jpg url=/5.jpg lastver= size=100 needRestart=true /
/updateFiles
同时,客户端也保存了一个需要升级的本地文件的列表,形式和服务器配置文件差不多,我们叫它本地配置文件。其中,Enable节点表示是否启用自动升级功能,ServerUrl表示服务器配置文件的地址。update.config
?xml version=1.0 encoding=utf-8?
Config xmlns:xsi=/2001/XMLSchema-instance xmlns:xsd=/2001/XMLSchema
Enabledtrue/Enabled
ServerUrl/updateService.xml/ServerUrl
/Config
使用自动组件的程序在启动时,会去检查这个配置文件。如果发现有配置文件中的文件版本和本地配置文件中描述的文件版本不一致,则提示用户下载。同时,如果本地配置文件中某些文件在服务器配置文件的文件列表中不存在,则说明这个文件已经不需要了,需要删除。最后,当升级完成后,会更新本地配置文件。我们先来看一下如何使用这个组件。在程序的Program.cs的Main函数中:[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
AutoUpdater au = new AutoUpdater();
try { au.Update(); }
catch (System.Net.WebException exception) { MessageBox.Show(无法找到指定资源\n\n + exception.Message, 自动升级, MessageBoxButtons.OK, MessageBoxIcon.Error); }
catch (System.Xml.XmlException exception) { MessageBox.Show(下载的升级文件有错误\n\n + exception.Message, 自动升级, MessageBoxButtons.OK, MessageBoxIcon.Error); }
catch (System.NotSupportedException exception) { MessageBox.Show(升级地址配置错误\n\n + exception.Message, 自动升级, MessageBoxButtons.OK, MessageBoxIcon.Error); }
catch (System.ArgumentException exception) { MessageBox.Show(下载的升级文件有错误\n\n + exception.Message, 自动升级, MessageBoxButtons.OK, MessageBoxIcon.Error); }
catch (System.Exception exception) { MessageBox.Show(升级过程中发生错误\n\n + exception.Message, 自动升级, MessageBoxButtons.OK, MessageBoxIcon.Error); }
}
如上所示,只需要简单的几行代码,就可以实现自动升级功能
原创力文档

文档评论(0)