国产探花免费观看_亚洲丰满少妇自慰呻吟_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);
       }
      }

    發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 丹阳市| 六盘水市| SHOW| 辉南县| 柳江县| 灵武市| 泗水县| 陈巴尔虎旗| 嘉禾县| 革吉县| 全南县| 平塘县| 镇原县| 称多县| 措美县| 龙州县| 辛集市| 安康市| 滨海县| 新民市| 呼玛县| 抚州市| 远安县| 吕梁市| 景洪市| 民权县| 河北省| 杭锦旗| 云梦县| 马山县| 蒲江县| 平泉县| 云和县| 镇平县| 潼南县| 新源县| 日照市| 洱源县| 镇江市| 来安县| 西华县|