zoukankan      html  css  js  c++  java
  • C#下实现在线升级

    这是一个webservice

    private AppUpdate.UpdateServ  UpdateSvr;


      private void button1_Click(object sender, System.EventArgs e)
      {
       
       if(LinkWebServices()==true)
       {
        this.label1.Text="连接服务器....... PASS";
        
        if(CheckVer()==true)
        {
         this.label2.Text="检查最新版本并下载.......PASS";
                      
        }
        else
        {
         this.label2.Text="检查最新版本并下载.......FAIL";
        }
       }
       else
       {
        this.label1.Text="连接服务器.......FAIL";
       }
      }

    //这是用来与升级服务器建立连接
      private bool LinkWebServices()
      {
       try
       {
        UpdateSvr=new UpdateServ();
        return true;
       }
       catch
       {
        return false;
       }
      }

    //调用webservice用来检查是不是有最新的版本
      private bool CheckVer()
      {
       string path =Application.StartupPath;
       try
       {
        VersionCheck(path);
        return true;
       }
       catch(Exception ex)
       {
        MessageBox.Show(ex.ToString());
        return false;
       }
      }

      private void VersionCheck(string desPath)
      {
       try
       {
        #region 查看文件和目录
        if(!desPath.EndsWith(@"\"))
         desPath += @"\";

        if(!System.IO.Directory.Exists(desPath))
        {
         System.IO.Directory.CreateDirectory(desPath);
        }

        string tempPath = desPath + @"tempDesPathCache\";

        if(System.IO.Directory.Exists(tempPath))
        {
         System.IO.Directory.Delete(tempPath,true);
         System.IO.Directory.CreateDirectory(tempPath);
        }
        else
         System.IO.Directory.CreateDirectory(tempPath);

        if(!System.IO.File.Exists(desPath + "UpdateConfig.xml"))
        {
         System.Xml.XmlDocument updateConfig = new System.Xml.XmlDocument();
         updateConfig.LoadXml(@"<root></root>");
         updateConfig.Save(desPath + "UpdateConfig.xml");
        }
        #endregion

       
        System.Xml.XmlDocument serverXmlDoc = UpdateSvr.AppUpdateVertion();
        System.Xml.XmlDocument localXmlDoc = new System.Xml.XmlDocument();
        localXmlDoc.Load(desPath + "UpdateConfig.xml");
        bool newVersionExist = false;
        bool moduleExist = false;
        System.Xml.XmlNode serverNode0 = serverXmlDoc.ChildNodes[0];
        System.Xml.XmlNode localNode0 = localXmlDoc.ChildNodes[0];
        foreach(System.Xml.XmlNode serverNode in serverNode0)
        {
         moduleExist = false;
         foreach(System.Xml.XmlNode localNode in localNode0)
         {
          //找到对应模块
          if(localNode.ChildNodes[0].InnerText == serverNode.ChildNodes[0].InnerText)
          {
           moduleExist = true;
           //版本号判断
           if(localNode.ChildNodes[1].InnerText.CompareTo(serverNode.ChildNodes[1].InnerText) < 0)
           {
            newVersionExist = true;
            if(System.Configuration.ConfigurationSettings.AppSettings["NetStyle"].ToString()=="internet")
            {
             DownloadFile(serverNode.ChildNodes[2].InnerText,tempPath + serverNode.ChildNodes[0].InnerText);
            }
            else
            {
             DownloadFile(serverNode.ChildNodes[3].InnerText,tempPath + serverNode.ChildNodes[0].InnerText);
            }
           }
           break;
          }
         }
         //没找到对应模块
         if(false == moduleExist)
         {
          
          if(System.Configuration.ConfigurationSettings.AppSettings["NetStyle"].ToString()=="internet")
          {
           DownloadFile(serverNode.ChildNodes[2].InnerText,tempPath + serverNode.ChildNodes[0].InnerText);
          }
          else
          {
           DownloadFile(serverNode.ChildNodes[3].InnerText,tempPath + serverNode.ChildNodes[0].InnerText);
          }
         }
        }
        //写入新UpdateConfig.xml升级完毕后替换
        if(newVersionExist)
        {
         serverXmlDoc.Save(tempPath + "UpdateConfig.xml");
         if(DialogResult.Yes == MessageBox.Show("有新版本,是否更新?","提示",MessageBoxButtons.YesNo))
         {
          string[] dirs = System.IO.Directory.GetFiles(tempPath, "*.*");
          string fileName;
          foreach (string dir in dirs)
          {
           fileName = ((dir.Split(Convert.ToChar(@"\")))[dir.Split(Convert.ToChar(@"\")).Length - 1]);
           if(System.IO.File.Exists(desPath + fileName))
           {
            //TODO:可以支持备份以前版本
            System.IO.File.Delete(desPath + fileName);
           }
           //TODO:如果系统正在运行,您得停止系统,至于如何停止,也许可以使用System.Diagnostics.Process
           System.IO.File.Move(dir,desPath + fileName);
          }
          MessageBox.Show("升级完毕");
         }
         else
         {
          //TODO:可以支持重新提示升级
         }
        }
       }
       catch(Exception ex)
       {
        throw new Exception("升级失败,原因是:" + ex.Message,ex);
       }
      }

    //下载最新的文件

      private void DownloadFile(string source,string fileName)
      {
       try
       {
        System.Net.WebClient myWebClient = new System.Net.WebClient();
        myWebClient.DownloadFile(source,fileName);
       }
       catch(Exception ex)
       {
        throw new Exception("下载失败,原因是:" + ex.Message,ex);
       }
      }


  • 相关阅读:
    day37 多路复用IO模型之select select,poll,epoll 异步IO 数据库基本概念 配置文件管理
    day36 协程介绍/yield实现协程/补充/gevent模块/网络IO模型介绍/阻塞IO模型/非阻塞IO模型
    day35 GIL介绍 cpython解释器的垃圾回收机制 GIL与自定义互斥锁 多进程vs多线程 线程queue 进程池与线程池
    day34 线程介绍/开启线程的两种方式/进程vs线程/线程对象其他相关属性或方法/互斥锁/死锁现象与递归锁/信号量
    day33 守护进程、互斥锁、进程间通信、生产者消费者模型part1、生产者消费者模型part2、生产者消费者模型part3
    day 32 进程理论/开启子进程的两种方式/僵尸进程与孤儿进程/进程间内存空间隔离/join操作/进程对象其他相关属性或方法
    day31 基于udp协议套接字通信 udp协议通信循环原理分析 udp协议没有粘包问题/基于socketserver模块实现并发的套接字(tcp) 基于socketserver模块实现并发的套接字(udp) 进程的简单介绍 操作系统的功能介绍 操作系统的发展史
    day30 模拟ssh远程执行命令/解决粘包执行问题/粘包终极解决方案/tcp的优化算法以及粘包问题分析
    Unity3D游戏开发之MatchTarget实现角色攀爬效果
    javase学习第九天(抽象类与接口)
  • 原文地址:https://www.cnblogs.com/goody9807/p/944020.html
Copyright © 2011-2022 走看看