首页 > 其他分享 >软件的自动更新实现

软件的自动更新实现

时间:2022-09-27 11:45:07浏览次数:43  
标签:xml string 实现 doc System Value Application 自动更新 软件

【转载自】https://www.cnblogs.com/meteorcui/archive/2005/11/16/2021123.html

1.思路: 使用WebService来实现这一功能,Webservice中放一xml文件,用于存储版本和需要更新的列表.客户端应用程序在每次启动时,访问webservice,检查version,如果版本低则下载xml文件,启动AutoUpdate.exe进行更新.

2.步骤:

2.1 定义WebService

2.1.1 定义xml文件

            <?xml version="1.0" encoding="utf-8" ?>

                <product>

                 <version>1.0.1818.42821</version>

                 <description>修正一些Bug</description>

                     <filelist count="2" sourcepath="./files/">

                          <item name="AutoUpdateTestDll.dll" size="">

                          <value />

                          </item>

 

                          <item name="AutoUpdateTest.exe" size="">

                          <value />

                          </item>

 

                      </filelist>

                 </product>

 

       2.1.2 定义WebMethod

             [WebMethod(Description="取得更新版本")]

       public string GetVersion()

       {

           XmlDocument doc = new XmlDocument();

           doc.Load(Server.MapPath("UpdateList.xml"));

           XmlElement root = doc.DocumentElement;

           return root.SelectSingleNode("version").InnerText;

       }

 

              [WebMethod(Description="获取更新的文件列表")]

       public string GetUpdateFile()

       {

           //取得更新的xml模板内容

           XmlDocument doc = new XmlDocument();

           doc.Load(Server.MapPath("UpdateList.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 = Convert.ToBase64String(br.ReadBytes((int)fs.Length),0,(int)fs.Length);

              br.Close();

              fs.Close();

           }

           return doc.OuterXml;

       }

    2.2 定义AutoUpdate.exe

       private void Form1_Load(object sender, System.EventArgs e)

       {

           //关闭当前程序.

           this.CloseApplication();

           //开始更新

           UpdateFile();

       }

       private void CloseApplication()

       {

           try

           {

              //关闭SM进程

              System.Diagnostics.Process[] process =

                  System.Diagnostics.Process.GetProcessesByName("AutoUpdateTest");

              foreach(System.Diagnostics.Process pro in process)

              {

                  if(!pro.CloseMainWindow())

                  {

                     pro.Kill();

                  }

                 

              }

           }

           catch(Exception)

           {

              Application.Exit();

           }

       }

      

       private void UpdateFile()

       {

           XmlDocument doc = new XmlDocument();

           doc.Load(Application.StartupPath + @"\UpdateList.xml");

           XmlElement root = doc.DocumentElement;

           XmlNode updateNode = root.SelectSingleNode("filelist");

           string path = updateNode.Attributes["sourcepath"].Value;

           int count = int.Parse(updateNode.Attributes["count"].Value);

           //progressbar

           this.progressBar1.Maximum = count;

           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.Text = "正在更新:" + fileName + " (" + itemNode.Attributes["size"].Value + ") ...";

              FileStream fs = File.Create(Application.StartupPath +"\\"+fileName);

               fs.Write(System.Convert.FromBase64String(itemNode.SelectSingleNode("value").InnerText),0,int.Parse(itemNode.Attributes["size"].Value));

              fs.Close();

              //

              this.progressBar1.PerformStep();

           }

           this.Text = "更新完成.";

           string path2 = Application.StartupPath +@"\UpdateList.xml";

           if(File.Exists(path2))

              File.Delete(path2);

           this.Text = "正在重新启动应用程序...";

           System.Diagnostics.Process.Start("AutoUpdateTest.exe");

           Close();

           Application.Exit();

 

       }

   2.3 主应用程序 ,引用WebService

               AutoUpdateService ser = new AutoUpdateService();

 

             private void Form1_Load(object sender, System.EventArgs e)

       {

           ser = new UpdateService.UpdateService();

           //访问服务器,检查版本是否一致

           if(!this.CheckVersion())

           {

              //版本不一致,提示用户更新.

              DialogResult res = MessageBox.Show(

                                   "发现新版本,是否更新?",

                                 "系统提示",

                                 MessageBoxButtons.YesNo,

                                 MessageBoxIcon.Question);

 

              if(res == DialogResult.Yes)

                 this.StartUpdate();//则启动更新程序

 

           }

 

       }

       private bool CheckVersion()

       {

           string ver = ser.GetVersion();

           if(Application.ProductVersion.CompareTo(ver)<=0)

              return false;

           else

              return true;

       }

       private void StartUpdate()

       {

           this.Close();

            MyProgressBar frm = new MyProgressBar();

           frm.Show();

 

           this.DownloadFile();

 

           frm.Close();

 

           System.Diagnostics.Process.Start(Application.StartupPath+"\\AutoUpdate.EXE");

 

           Application.Exit();

       }

       private void DownloadFile()

       {

           string xml = ser.GetUpdateFile();

           System.Xml.XmlDocument doc = new System.Xml.XmlDocument();

           doc.LoadXml(xml);

           doc.Save(Application.StartupPath+"\\UpdateList.xml");

 

       }

标签:xml,string,实现,doc,System,Value,Application,自动更新,软件
From: https://www.cnblogs.com/topboy168/p/16733999.html

相关文章

  • 为什么同行业,同个软件,有些 ERP 成功,有的失败了?
    企业的差异性是各类系统部署必须正视的关键问题!同行业,同个软件,有些ERP成功,有的失败,基本上是企业差异性没有得到重视的,所以一点也不应该感到奇怪。规模不同、行业不同、发......
  • python 实现发送邮件功能
    一、前言   在开始正题之前,我们先理一下常见的电子邮件协议: SMTP、POP、IMAP都遵循TCP/IP协议规范。至于Exchange是邮件服务器,不是收邮件和发邮件的协议,不要混淆概......
  • CSS实现一个简单的聊天气泡
    问题描述:想实现一个聊天气泡框时需要一个突出的尖角,于是想到了借助伪元素来实现实现目标:实现一个可以动态变长的聊天气泡(如图所示)   实现过程:版本一:使用absout+......
  • Spring Boot中单例类实现对象的注入方式
    SpringBoot单例类实现对象的注入1、最近接手了一个项目项目用的是SpringBoot,但其中有个类用的是单例,为了不改变单例,且还需要引入Spring管理的Bean对象2、对于一个单例......
  • Ableton Live 11 Suite:强大的音乐创作软件
    AbletonLive11Suite是由来自国外Ableton公司的一款旗舰级音乐创作软件!新发布的Live11添加了许多要求很高的功能,例如优雅的编曲系统和对MPE的支持,还有新设备以及现......
  • 如何实现数据库读一致性
    1导读数据的一致性是数据准确的重要指标,那如何实现数据的一致性呢?本文从事务特性和事务级别的角度和大家一起学习如何实现数据的读写一致性。2一致性1.数据的一致性:通......
  • C# WinForm国际化实现的简单方法
    本文是完善:https://www.jb51.net/article/45675.htm文章描述很详细,重要的代码都贴出来了我帮大家懒到家,将窗口代码和资源文件也传上来到手可运行,直观的了解多语......
  • 在 WPF 中实现融合效果
    1.融合效果融合效果是指对两个接近的元素进行高斯模糊后再提高对比度,使它们看上去“粘”在一起。在之前的一篇文章中,我使用Win2D实现了融合效果,效果如下:不过Win2D......
  • 计算空间物体包围球的两种算法实现_charlee44的博客
    1.概述在进行二维空间几何运算的之前,往往会用包围盒进行快速碰撞检测,从而筛掉一些无法碰撞到的可能。而在三维中,比较常用的就是包围球了。当然,如何计算包围球是一个问题......
  • java 使用implements接口实现sort
    一、java是单继承,implements可以有多个我们可以以这样子的格式进行集成和尊从接口:classChild extendsParent implementsprotocol1, protocol2{};Child类继承了Pa......