国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

用C#實現在線升級

2024-07-21 02:26:44
字體:
來源:轉載
供稿:網友
  • 本文來源于網頁設計愛好者web開發社區http://www.html.org.cn收集整理,歡迎訪問。
  • 用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);
       }
      }

    發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 新邵县| 新昌县| 綦江县| 若羌县| 海丰县| 来安县| 梅河口市| 宁乡县| 濮阳县| 无锡市| 巴塘县| 佛教| 满洲里市| 景洪市| 二手房| 车险| 兰西县| 来安县| 塔河县| 英吉沙县| 新晃| 青冈县| 德庆县| 含山县| 德令哈市| 宝兴县| 东乌| 虹口区| 大冶市| 安龙县| 娄烦县| 萝北县| 青浦区| 伊通| 噶尔县| 元氏县| 醴陵市| 水富县| 寿阳县| 承德县| 磐安县|