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

首頁 > 編程 > C# > 正文

C# Winform 自動更新程序實例詳解

2020-01-24 00:23:59
字體:
來源:轉載
供稿:網友

本文實例為大家分享了C# Winform 自動更新程序,供大家參考,具體內容如下

第一步:檢查更新

檢查更新其實無非就是去比較更新包的版本和本地軟件版本,如果高則更新、低則不更新。怎么獲取版本號方法很多,本案例是獲取軟件的配置文件。

private bool CheckUpdate()    {      bool result = false;      try      {        string Cfg = TxtRead(exePath  "http://Config.txt");        ConfigLocal = JsonConvert.DeserializeObject<DTO_Config>(Cfg);         CheckUpdateURL = ConfigLocal.AutoUpdateURL;         Cfg = TxtRead(CheckUpdateURL  "http://Config.txt");        ConfigRemote = JsonConvert.DeserializeObject<DTO_Config>(Cfg);                 VersionR = ConfigRemote.Version;        VersionL = ConfigLocal.Version;        int VersionRemote = int.Parse(ConfigRemote.Version.Replace(".", ""));        int VersionLocal = int.Parse(ConfigLocal.Version.Replace(".", ""));         result = VersionRemote > VersionLocal;      }      catch { }      return result;    }

第二步:下載更新包

因為C/S的軟件更新是面對所有用戶,S端除了給C端提供基本的服務外,還可以給C端提供更新包。而這個S端可以是網絡上的一個固定地址,也可以是局域網內一個公共盤。那下載更新包無非就是去訪問服務端的文件,然后Copy下來或下載下來。下面給出訪問網絡和訪問局域網兩個案例:

A、訪問遠程網絡地址這里采用的是WebClient

public void DownLoadFile()    {      if (!Directory.Exists(UpdateFiles))      {        Directory.CreateDirectory(UpdateFiles);      }      using (WebClient webClient = new WebClient())      {        try        {          webClient.DownloadFileCompleted = new AsyncCompletedEventHandler(client_DownloadFileCompleted);          webClient.DownloadProgressChanged = new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);          webClient.DownloadFileAsync(new Uri(CheckUpdateURL  "http://UpdateFile.rar"), UpdateFiles  "http://UpdateFile.rar");        }        catch (WebException ex)        {          MessageBox.Show(ex.Message, "系統提示", MessageBoxButtons.OK, MessageBoxIcon.Error);        }      }    }

這里面應用到兩個方法,DownloadProgressChanged,監聽異步下載的進度;DownloadFileCompleted,監聽完成異步文件下載;

private void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)    {      this.progressBarUpdate.Minimum = 0;      this.progressBarUpdate.Maximum = (int)e.TotalBytesToReceive;      this.progressBarUpdate.Value = (int)e.BytesReceived;      this.lblPercent.Text = e.ProgressPercentage  "%";    }
private void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)    {      if (e.Error != null)      {        MessageBox.Show(e.Error.Message, "系統提示", MessageBoxButtons.OK, MessageBoxIcon.Error);      }      else      {        this.lblMessage.Text = "下載完成";        //復制更新文件替換舊文件        DirectoryInfo TheFolder = new DirectoryInfo(UpdateFiles);        foreach (FileInfo NextFile in TheFolder.GetFiles())        {          File.Copy(NextFile.FullName, Application.StartupPath  NextFile.Name, true);        }              }    }

B、訪問服務端公共盤,直接采用File.Copy

public void GetRemoteFile()    {      try      {        DirectoryInfo TheFolder = new DirectoryInfo(CheckUpdateURL);        FileInfo[] FileList = TheFolder.GetFiles();        this.progressBarUpdate.Minimum = 0;        this.progressBarUpdate.Maximum = FileList.Length;         foreach (FileInfo NextFile in FileList)        {          if (NextFile.Name != "Config.txt")          {            File.Copy(NextFile.FullName, exePath  "http://"  NextFile.Name, true);                      }          this.lblMessage.Text = "更新"  NextFile.Name;          this.progressBarUpdate.Value = 1;          this.lblPercent.Text = "更新進度... "  (this.progressBarUpdate.Value / FileList.Length) * 100  "%";        }        this.lblMessage.Text = "更新完成";        //更改本地版本號為最新版本號        ConfigLocal.Version = VersionR;        string cfgs = JsonConvert.SerializeObject(ConfigLocal);        TxtWrite(Application.StartupPath  "http://Config.txt", cfgs);      }      catch (Exception ex)      {        MessageBox.Show(ex.Message, "系統提示", MessageBoxButtons.OK, MessageBoxIcon.Error);      }    }

第三步:替換本地文件

這一步或許在第二步中已經實現了,如果你采用的是File.Copy。替換也就是復制粘貼的問題。采用WebClient下載了zip包,那還需解壓一下壓縮包然后再File.Copy。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 桐庐县| 碌曲县| 交城县| 景东| 昌宁县| 怀化市| 莆田市| 新平| 东兴市| 江油市| 荣昌县| 平乐县| 洪江市| 张家口市| 九龙坡区| 松潘县| 汤阴县| 高要市| 永安市| 德保县| 宁武县| 乡城县| 沧源| 隆德县| 闸北区| 大冶市| 曲靖市| 延吉市| 保康县| 漳平市| 洛扎县| 财经| 常德市| 枣阳市| 镇沅| 平泉县| 古丈县| 古丈县| 民县| 扶绥县| 南昌市|