在WinForm中使用Web Services 来实现 软件 自动升级( Auto Update ) (C#)

实现原理:在WebServices中实现一个GetVerWebMethod方法,其作用是获取当前的最新版本。
 
  然后将现在版本与最新版本比较,如果有新版本,则进行升级。

  步骤:
    1、准备一个XML文件 (Update.xml)
<?xml version="1.0" encoding="utf-8" ?>
<product>
 <version>1.0.1818.42821</version>
 <description>
修正一些Bug</description>
 <filelist count="4" sourcepath="./update/">
  <item name="City.xml" size="">
   <value />
  </item>
  <item name="CustomerApplication.exe" size="">
   <value />
  </item>
  <item name="Interop.SHDocVw.dll" size="">
   <value />
  </item>
  <item name="Citys.xml" size="">
   <value />
  </item>
 </filelist>
</product>
作用是作为一个升级用的模板。
    2、WebServicesGetVer方法。

               [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、WebServicesGetUpdateData方法。
    
    
               [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 = Convert.ToBase64String(br.ReadBytes((int)fs.Length),0,(int)fs.Length);
    
    
                               br.Close();
    
    
                               fs.Close();
    
    
                       }
    
    
                       return doc;
    
    
               }
    
    

    4、在客户端进行的工作。
      首先引用此WebServices,例如命名为:WebSvs
     string nVer = Start.GetService.GetVer(); 
     if(Application.ProductVersion.CompareTo(nVer)<=0)
      update();

  在本代码中 Start.GetServiceWebSvs的一个Static 实例。首先检查版本,将结果与当前版本进行比较,
如果为新版本则执行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();
    
    
               }
这里为了简单起见,没有使用异步方法,当然使用异步方法能更好的提高客户体验,这个需要读者们自己去添加。
:) update的作用是将升级的XML文件下载下来,保存为执行文件目录下的一个Update.xml文件。任务完成,
退出程序,等待Update.Exe 来进行升级。
 
    
    
    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),0,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();
    
    
               }
    
    

    
    
这个代码也很容易懂,首先就是找到主进程,如果没有关闭,则用Process.Kill()来关闭主程序。然后则用一个
XmlDocumentLoad程序生成的update.xml文件。用xml文件里指定的路径和文件名来生成指定的文件,在这之
前先前已经存在的文件删除。更新完毕后,则重新启动主应用程序。这样更新就完成了。
     
     
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值