软件的自动更新实现

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");

 

       }

posted @ 2005-11-16 17:42  meteorcui  阅读(953)  评论(0编辑  收藏  举报