C# Winform 自動更新程序實例


第一步:檢查更新

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

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端可以是網絡上的一個固定地址,也可以是局域網內一個公共盤。那下載更新包無非就是去訪問服務端的文件,然后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   "\\UpdateFile.rar"), UpdateFiles   "\\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   "\\"   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, "系統提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

第三步:替換本地文件

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM