软件的自动更新实现

1.思路: 使用WebService来实现这一功能,Webservice中放一xml文件,用于存储版本和需要更新的列表.客户端应用程序在每次启动时,访问webservice,检查version,如果版本低则下载xml文件,启动AutoUpdate.exe进行更新.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

 

2.步骤:

2.1 定义WebService

<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />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");

 

       }

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值