winform程序相对web程序而言,功能更强大,编程更方便,但软件更新却相当麻烦,要到客户端一台一台地升级,面对这个实际问题,在最近的一个小项目中,本人设计了一个通过软件实现自动升级技术方案,弥补了这一缺陷,有较好的参考价值。
一、升级的好处。
长期以来,广大程序员为到底是使用Client/Server,还是使用browser/Server结构争论不休,在这些争论当中,C/S结构的程序的可维护性差,布置困难,升级不方便,维护成本高就是一个相当重要的因素,也是那些B/S的支持者们将Client/Server结构打入地狱的一个重要原因。
二、升级的技术原理。
三、在.Net时代的实现。
然后将现在版本与最新版本比较,如果有新版本,则进行升级。
步骤:
- <?xml version=@H_502_85@"1.0" encoding=@H_502_85@"utf-8" ?>
- <product>
- <version>1.0.1818.42821</version>
- <description>修正一些Bug</description>
- <filelist count=@H_502_85@"4" sourcepath=@H_502_85@"./update/">
- <item name=@H_502_85@"City.xml" size=@H_502_85@"">
- <value />
- </item>
- <item name=@H_502_85@"CustomerApplication.exe" size=@H_502_85@"">
- <value />
- </item>
- <item name=@H_502_85@"Interop.SHDocVw.dll" size=@H_502_85@"">
- <value />
- </item>
- <item name=@H_502_85@"Citys.xml" size=@H_502_85@"">
- <value />
- </item>
- </filelist>
- </product>
2、WebServices的GetVer方法。
- [WebMethod(Description="取得更新版本")]
- public string GetVer()
- {
- XmlDocument doc = new XmlDocument();
- doc.Load(Server.MapPath("update.xml"));
- XmlElement root = doc.DocumentElement;
- return root.SelectSingleNode("version").InnerText;
- }
3、WebServices的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<count;i++)
- {
- XmlNode itemNode = 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 =
- onvert.ToBase64String(br.ReadBytes((int)fs.Length),(int)fs.Length);
- br.Close();
- fs.Close();
- }
- return doc;
- }
4、在客户端进行的工作。
首先引用此WebServices,例如命名为:WebSvs,
- string nVer = Start.GetService.GetVer();
- if(Application.ProductVersion.Compareto(nVer)<=0)
- update();
如果为新版本则执行UpDate方法。
- 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 的内容。
- private void Form1_Load(object sender, System.EventArgs e)
- {
- System.Diagnostics.Process[] ps = System.Diagnostics.Process.GetProcesses();
- foreach(System.Diagnostics.Process p in ps)
- {
- //MessageBox.Show(p.ProcessName);
- if(p.ProcessName.ToLower()=="customerapplication")
- {
- p.Kill();
- break;
- }
- }
- XmlDocument doc = new XmlDocument();
- doc.Load(Application.StartupPath + @"/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);
- for(int i=0;i<count;i++)
- {
- XmlNode itemNode = updateNode.ChildNodes[i];
- string fileName = itemNode.Attributes["name"].Value;
- FileInfo fi = new FileInfo(fileName);
- fi.Delete();
- //File.Delete(Application.StartupPath + @"/" + fileName);
- this.label1.Text = "正在更新: " + fileName + " (" + itemNode.Attributes["size"].Value + ") ...";
- FileStream fs = File.Open(fileName,FileMode.Create,FileAccess.Write);
- fs.Write(System.Convert.FromBase64String(itemNode.SelectSingleNode("value").InnerText),int.Parse(itemNode.Attributes["size"].Value));
- fs.Close();
- }
- label1.Text = "更新完成";
- File.Delete(Application.StartupPath + @"/update.xml");
- label1.Text = "正在重新启动应用程序...";
- System.Diagnostics.Process.Start("CustomerApplication.exe");
- Close();
- Application.Exit();
- }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。