[本篇论文由上帝论文网为您收集整理,上帝论文网http://paper.5var.com将为您整理更多优秀的免费论文,谢谢您的支持]利用Web Services实现软件自动升级 曹庆旭 (黔东南民族职业技术学院,贵州,凯里,556000) 摘 要:软件维护升级工作是软件生命周期最重要的环节。为了解决以往C/S(Client/Server)模式下的客户端软件升级效率低的问题,设计了C/S应用系统自动升级处理程序。该程序利用Web Services技术、C#和XML语言,通过网络来完成C/ S应用系统的自动升级。与原有手工升级、FTP 文件服务器升级和第三方控件升级相比,升级效率更高。该方案具有较好的参考价值。 关键词:C#;Web Services;XML;软件自动升级 中图法分类号: 文献标志码: 1 引言 随着计算机网络应用技术的不断发展,在开发MIS系统时,大多采用基于C/S(客户机/服务器)模式或B/S(浏览器/服务器)模式。现在B/S模式以其真正意义上的瘦客户机/胖服务器模式优势占据了主导地位。但是由于客户机/服务器模式具有的数据流量小、响应时间短、安全性高等特点,在解决几十个到几百个用户的局域网中,仍然是一个不错的选择[1-3]。在C/S模式下,应用程序的每次升级都需要在每个客户端重新安装应用程序,这是一项十分繁琐的事情。面对这个实际问题,这里设计了一个通过软件实现自动升级技术方案,弥补了这一缺陷,有较好的参考价值。 2 设计思路 判断一个文件是否要更新,可以通过判断文件的大小、修改日期和文件的版本号来实现[3-5]。发现最新的则提示用户是否升级。 在Web Services中实现一个GetVer的WebMethod方法,其作用是获取当前的最新版本。然后将现在版本与最新版本比较,如果有新版本,则进行升级。 3 自动升级的技术实现 (1)编写升级模板文件Update.xml 准备一个XML文件 (Update.xml) ,作为一个升级用的模板。 …… <description>升级记录</description> <filelist count="4" sourcepath="./update/"> …… <item name="CustomerApplication.exe" size=""> <value /> </item> <item name="Interop.SHDocVw.dll" size=""> <value /> </item> …… SHDocVw.DLL 是Internet Explorer的一个组件,该组件负责控制对从Web 站点返回的URL和信息的处理。 (2)编写Web Services的GetVer方法 GetVer方法用于取得软件的更新版本。 [WebMethod(Description="取得更新版本")] Public string GetVer() { XmlDocument doc = new XmlDocument(); doc.Load(Server.MapPath("update.xml")); XmlElement root = doc.DocumentElement; return root.SelectSingleNode("version").Inertest; } (3)编写Web Services的GetUpdateData方法 GetUpdateData方法用于在线更新软件。 [WebMethod(Description="在线更新软件")] [SoapHeader("sHeader")] public System.Xml.XmlDocument GetUpdateData() { //验证用户是否登陆 if(sHeader==null) return null; if(!DataProvider.GetInstance.CheckLogin(sHeader.Username,sHeader.Password)) return null; //取得更新的xml模板内容 XmlDocument doc = new XmlDocument(); doc.Load(Server.MapPath("update.xml")); XmlElement root = doc.DocumentElement; //看看有几个文件需要更新 XmlNode updateNode = root.SelectSingleNode("filelist"); string path = updateNode.Attributes["sourcepath"].Value; int count = int.Parse(updateNode.Attributes["count"].Value); //将xml中的value用实际内容替换 for(int i=0;i= updateNode.ChildNodes[i]; string fileName = path + itemNode.Attributes["name"].Value; FileStream fs = File.OpenRead(Server.MapPath(fileName)); itemNode.Attributes["size"].Value = fs.Length.ToString(); BinaryReader br = new BinaryReader(fs); //这里是文件的实际内容,使用了Base64String编码 itemNode.SelectSingleNode("value").InnerText = Convert.ToBase64String(br.ReadBytes((int)fs.Length),0,(int)fs.Length); br.Close(); fs.Close(); } return doc; }
(4)编写客户端的UpDate方法 首先引用此Web Services,例如命名为:WebSvs, string nVer = Start.GetService.GetVer(); if(Application.ProductVersion.CompareTo(nVer)<=0) update(); 在本代码中Start.GetService是WebSvs的一个静态实例。功能是:首先检查版本,将结果与当前版本进行比较,如果为新版本则执行UpDate方法。 update的作用是将升级的XML文件下载下来,保存为执行文件目录下的一个Update.xml文件。任务完成,退出程序,等待Update.Exe 来进行升级。 void update() { this.statusBarPanel1.Text = "正在下载..."; System.Xml.XmlDocument doc = ((System.Xml.XmlDocument)Start.GetService.GetUpdateData()); doc.Save(Application.StartupPath + @"\update.xml"); System.Diagnostics.Process.Start(Application.StartupPath + @"\update.exe"); Close(); Application.Exit(); } (5)编写客户端的Update.Exe Update.exe的功能主要是:首先就是找到主进程;如果没有关闭,则用Process.Kill()来关闭主程序。然后则用一个XmlDocument来Load程序生成的update.xml文件。用xml文件里指定的路径和文件名来生成指定的文件,在这之前先前已经存在的文件删除。更新完毕后,则重新启动主应用程序。这样更新就完成了。 private void Form1_Load(object sender, Sys [1] [2] 下一页
Tags:
|