欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C#教程

當前位置:主頁 > 軟件編程 > C#教程 >

C# Winform 自動更新程序?qū)嵗斀?/h1>
來源:本站原創(chuàng)|時間:2020-01-10|欄目:C#教程|點擊: 次

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

第一步:檢查更新

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

private bool CheckUpdate()
    {
      bool result = false;
      try
      {
        string Cfg = TxtRead(exePath  "\\Config.txt");
        ConfigLocal = JsonConvert.DeserializeObject<DTO_Config>(Cfg);
 
        CheckUpdateURL = ConfigLocal.AutoUpdateURL;
 
        Cfg = TxtRead(CheckUpdateURL  "\\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端可以是網(wǎng)絡上的一個固定地址,也可以是局域網(wǎng)內(nèi)一個公共盤。那下載更新包無非就是去訪問服務端的文件,然后Copy下來或下載下來。下面給出訪問網(wǎng)絡和訪問局域網(wǎng)兩個案例:

A、訪問遠程網(wǎng)絡地址這里采用的是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  "\\UpdateFile.rar"), UpdateFiles  "\\UpdateFile.rar");
        }
        catch (WebException ex)
        {
          MessageBox.Show(ex.Message, "系統(tǒng)提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
      }
    }

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

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, "系統(tǒng)提示", 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  "\\"  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  "\\Config.txt", cfgs);
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.Message, "系統(tǒng)提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
      }
    }

第三步:替換本地文件

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

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持我們。

網(wǎng)頁制作CMS教程網(wǎng)絡編程軟件編程腳本語言數(shù)據(jù)庫服務器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有