C # Winform automatically updates program instances,
Step 1: Check for updates
Checking for updates is nothing more than comparing the version of the update package and the local software version. If the update is high, the update is not updated. There are many ways to get the version number. In this case, the configuration file of the software is obtained.
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; }
Step 2: Download the update package
C/S software updates are intended for all users. In addition to providing basic services to C, S can also provide update packages to C. This send can be a fixed address on the network or a public disk in the LAN. Download the update package is nothing more than accessing the server file, and then Copy or download it. The following are two examples of network access and LAN access:
A. Access the remote network address. WebClient is used here.
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, "system prompt", MessageBoxButtons. OK, MessageBoxIcon. error );}}}
Two methods are applied here: DownloadProgressChanged, listening for the asynchronous download progress; DownloadFileCompleted, listening for asynchronous file download;
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, "system prompt", MessageBoxButtons. OK, MessageBoxIcon. error);} else {this. lblMessage. text = "downloaded"; // Replace the updated file with the old file DirectoryInfo TheFolder = new DirectoryInfo (UpdateFiles); foreach (FileInfo NextFile in TheFolder. getFiles () {File. copy (NextFile. fullName, Application. startupPath NextFile. name, true );}}}
B. Access the public disk on the server and directly use 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 = "Update" NextFile. name; this. progressBarUpdate. value = 1; this. lblPercent. text = "update progress... "(this. progressBarUpdate. value/FileList. length) * 100 "%";} this. lblMessage. text = "updated"; // change the local version to the latest version: ConfigLocal. version = VersionR; string cfgs = JsonConvert. serializeObject (ConfigLocal); TxtWrite (Application. startupPath "\ Config.txt", cfgs);} catch (Exception ex) {MessageBox. show (ex. message, "system prompt", MessageBoxButtons. OK, MessageBoxIcon. error );}}
Step 3: replace the local file
This step may have been implemented in step 2 if File. Copy is used. Replace: copy and paste. If you use WebClient to download the zip package, you need to decompress the package and then File. Copy.