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

首頁 > 開發 > 綜合 > 正文

C#下實現在線升級

2024-07-21 02:17:38
字體:
來源:轉載
供稿:網友

//這是一個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);
   }
  }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 澄城县| 逊克县| 贵阳市| 阿拉善左旗| 富锦市| 关岭| 乃东县| 含山县| 丹阳市| 深州市| 天津市| 东台市| 肥西县| 山阴县| 巴林左旗| 天气| 临海市| 三门峡市| 东安县| 永济市| 青铜峡市| 新田县| 焉耆| 虞城县| 石棉县| 昆明市| 汝阳县| 寿光市| 衡山县| 安国市| 元江| 北宁市| 蕉岭县| 昌图县| 禹州市| 屯昌县| 宝兴县| 宁都县| 黎城县| 抚顺市| 绩溪县|