C#製作線上升級程式

來源:互聯網
上載者:User

標籤:des   style   blog   color   使用   os   檔案   io   

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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.