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

首頁(yè) > 編程 > C# > 正文

C#實(shí)現(xiàn)在線更新軟件

2020-01-24 01:48:59
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

通過(guò)某些手段后臺(tái)更新軟件。首先你要有一個(gè)放置新版本信息的網(wǎng)站

UpdateSoftwareForm.cs

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using CCWin;using System.Net;using System.Collections;using System.IO;using System.Xml;using System.Diagnostics;using System.Threading; namespace WriteBook{  public partial class UpdateSoftwareForm : Skin_Metro  {    public UpdateSoftwareForm()    {      InitializeComponent();    }     #region 一些對(duì)象和變量     //使用WebClient下載    WebClient client = new WebClient();    ArrayList downlist = new ArrayList();    //當(dāng)前版本    string nowversion = null;    //最新版本    string latesversion = null;     #endregion     #region 獲取版本號(hào)     /// <summary>    /// 從服務(wù)器上獲取最新的版本號(hào)    /// </summary>    public void DownloadCheckUpdateXml()    {      try      {        //第一個(gè)參數(shù)是文件的地址,第二個(gè)參數(shù)是文件保存的路徑文件名        client.DownloadFile("http://bbs.cloudtour.tk/SoftwareDownload/WriteBook/WriteBook2.xml", "WriteBook2.xml");      }      catch      {        MessageBox.Show("沒(méi)有檢測(cè)到更新。", "提示");        this.Close();      }    }     /// <summary>    /// 獲取本地軟件的版本號(hào)    /// </summary>    private void NowVersion()    {      nowversion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString() + "/n";      LocalText.Text = nowversion;    }     /// <summary>    /// 讀取從服務(wù)器獲取的最新版本號(hào)    /// </summary>    public void LatestVersion()    {      try      {        if (File.Exists("WriteBook2.xml.xml"))        {          XmlDocument doc = new XmlDocument();          //加載要讀取的XML          doc.Load("WriteBook2.xml.xml");           //獲得根節(jié)點(diǎn)          XmlElement WriteBook = doc.DocumentElement;           //獲得子節(jié)點(diǎn) 返回節(jié)點(diǎn)的集合          XmlNodeList Update = WriteBook.ChildNodes;           foreach (XmlNode item in Update)          {            latesversion = item.InnerText;          }          LatestText.Text = latesversion;        }        else        {          MessageBox.Show("沒(méi)有檢測(cè)到更新。", "提示");          this.Close();        }      }      catch      {        this.Close();      }    }     #endregion     #region 初始化程序     /// <summary>    /// 初始化程序    /// </summary>    private void InitializeandInstall()    {      UpdateProgressBar.Value = 20;      DownloadCheckUpdateXml();      UpdateProgressBar.Value = 40;      NowVersion();      UpdateProgressBar.Value = 60;      LatestVersion();      UpdateProgressBar.Value = 80;      DownloadInstall();      UpdateProgressBar.Value = 100;    }     #endregion     #region 安裝and刪除     /// <summary>    /// 下載安裝包    /// </summary>    public void DownloadInstall()    {      try      {        if (nowversion == latesversion)        {          MessageBox.Show("您已經(jīng)是最新版本。", "提示");        }        else if (nowversion != latesversion && File.Exists("WriteBook2.xml"))        {          MessageBox.Show("發(fā)現(xiàn)新版本,即將下載更新補(bǔ)丁。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);          client.DownloadFile("http://bbs.cloudtour.tk/SoftwareDownload/WriteBook/WBsetup.exe", "WBsetup.exe");          if (File.Exists("Setup.exe"))          {            InstallandDelete();          }          else          {            for (int i = 1; i < 3; i++)            {              client.DownloadFile("http://bbs.cloudtour.tk/SoftwareDownload/WriteBook/WBsetup.exe", "WBsetup.exe");            }            MessageBox.Show("下載失敗,請(qǐng)檢查您的網(wǎng)絡(luò)連接是否正常。", "提示");            this.Close();          }        }      }      catch      {        MessageBox.Show("更新失敗,沒(méi)有發(fā)現(xiàn)新版本。", "提示");        this.Close();      }    }     /// <summary>    /// 安裝及刪除    /// </summary>    private void InstallandDelete()    {      try      {        DialogResult dr = MessageBox.Show("下載更新成功,是否安裝新更新?", "提示", MessageBoxButtons.YesNoCancel);        if (dr == System.Windows.Forms.DialogResult.Yes)        {          //啟動(dòng)安裝程序          System.Diagnostics.Process.Start("WBsetup.exe");          Thread td = new Thread(JudgeInstall);          td.Start();        }        else { }      }      catch      {        MessageBox.Show("發(fā)生未知錯(cuò)誤,更新失敗。", "提示");        this.Close();      }    }     /// <summary>    /// 判斷安裝進(jìn)程是否存在    /// </summary>    public void JudgeInstall()    {      while (true)      {        Process[] processList = Process.GetProcesses();        foreach (Process process in processList)        {          if (process.ProcessName == "WBsetup.exe") { }          else          {            DialogResult dr = MessageBox.Show("更新成功,是否刪除安裝包?", "提示", MessageBoxButtons.YesNo);            if (dr == System.Windows.Forms.DialogResult.Yes)            {              File.Delete("WBsetup.exe");              File.Delete("WriteBook2.xml");            }          }        }      }    }     #endregion     /// <summary>    /// 點(diǎn)擊初始化程序    /// </summary>    /// <param name="sender"></param>    /// <param name="e"></param>    private void UpdateButton_Click(object sender, EventArgs e)    {      InitializeandInstall();    }  }}

以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 泊头市| 图木舒克市| 清流县| 深泽县| 云和县| 北川| 崇仁县| 秭归县| 永平县| 饶平县| 仪征市| 延寿县| 丹东市| 潞西市| 斗六市| 新乡市| 博野县| 饶平县| 华宁县| 莱芜市| 奎屯市| 天台县| 治县。| 达拉特旗| 弥勒县| 甘洛县| 禄劝| 保定市| 铁岭县| 呼和浩特市| 五华县| 饶平县| 新巴尔虎左旗| 郓城县| 潼南县| 丰城市| 安西县| 广德县| 牟定县| 阜新市| 崇明县|