Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the automatic upgrade Program of Winform in C #

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article is to share with you about how to implement Winform automatic upgrade program in C #. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Development

Third-party toolkit

Create a new WinForm project and name it SumUpdater. The following picture shows the directory of the entire project.

In the upgrade program, we need to check the version information comparison. I use JSON data in the background TXT file, and I have to extract the ZIP file after download, so we need to refer to third-party programs Newtonsoft.Json and DotNetZip.

Select manage NuGet package with the left mouse button in the reference

Search for Newtonsoft.Json and DotNetZip installation

Main interface

Rename the main form to MainForm.cs, and then add two controls to the interface, one label and one progressbar.

Then rewrite the constructor of the main window

Public MainForm (string serverIP, int serverPort, string _ callBackExeName, string title, int oldversioncode)

Added five parameters: server IP address, port number, program name after upgrade, title information and current version number.

App.config

Add several items to the local config file, which are used to set the server's IP address, port number, EXE program to be called after the upgrade, and the current version number.

Then add the parameters to read this information in the Program.cs startup item, and then pass it to the main form

Static void Main ()

{

Try

{

Application.EnableVisualStyles ()

Application.SetCompatibleTextRenderingDefault (false)

String serverIP = ConfigurationManager.AppSettings ["ServerIP"]

Int serverPort = int.Parse (ConfigurationManager.AppSettings ["ServerPort"])

String callBackExeName = ConfigurationManager.AppSettings ["CallbackExeName"]

String title = ConfigurationManager.AppSettings ["Title"]

Int VersionCode = int.Parse (ConfigurationManager.AppSettings ["Version"])

MainForm form = new MainForm (serverIP, serverPort, callBackExeName, title, VersionCode)

Application.Run (form)

}

Catch (Exception ex)

{

MessageBox.Show (ex.ToString ())

}

Detect and download update Updater.cs

We use WebClient to communicate with the server.

The two main methods in this class are GetUpdaterInfo () and DownLoadUpGrade (string url)

/ / /

/ / detect upgrade information

/ / /

/ / /

/ / /

/ / /

Public void GetUpdaterInfo ()

{

Info = new CUpdInfo ()

_ client = new WebClient ()

/ / get the string _ checkurl that detects the upgrade as the address

String json = _ client.DownloadString (_ checkurl)

/ / serialize json

Info = SerializationHelper.Deserialize (json, 0)

/ / determine that if the version number on the server is greater than the local version number, execute DownLoadUpGrade (). The parameter is the info.appdownloadurl download address.

If (info.versionCode > _ oldversioncode)

{

DownLoadUpGrade (info.appdownloadurl)

}

Else

{

_ lbltext.Text = "currently the latest version, no need to upgrade!"

/ / start the original program directly after waiting for 500 milliseconds

Thread.Sleep (1500)

UpdaterOver.StartApp (_ appFileName)

}

}

/ / /

/ / download the upgrade file

/ / /

/ / /

/ / /

Public void DownLoadUpGrade (string url)

{

_ client = new WebClient ()

If (_ client.IsBusy)

{

_ client.CancelAsync ()

}

_ client.DownloadProgressChanged + =

New DownloadProgressChangedEventHandler (webClient_DownloadProgressChanged)

_ client.DownloadFileCompleted + = new AsyncCompletedEventHandler (webClient_DownloadFileCompleted)

/ / start downloading

_ client.DownloadFileAsync (new Uri (url), _ downfilename)

}

/ / /

/ / download the progress bar

/ / /

/ / /

/ / /

Private void webClient_DownloadProgressChanged (object sender, DownloadProgressChangedEventArgs e)

{

_ progressBar.Value = e.ProgressPercentage

_ lbltext.Text = $"downloading file, completion progress {e.BytesReceived} / {e.TotalBytesToReceive}"

}

/ / /

/ / events after the download is completed

/ / /

/ / /

/ / /

Private void webClient_DownloadFileCompleted (object sender, AsyncCompletedEventArgs e)

{

If (e.Cancelled)

{

_ lbltext.Text = "download canceled!"

}

Else

{

_ lbltext.Text = "download complete!"

Try

{

Thread.Sleep (1000)

UpdaterOver.StartOver (_ downfilename, _ appDirPath, info.versionCode, _ appFileName)

}

Catch (Exception ex)

{

_ lbltext.Text = ex.Message

}

}

}

Download completed UpdaterOver.cs

/ / /

/ / /

/ / /

/ / /

/ / /

Public static void StartOver (string zipfile, string destpath, int versioncode, string appfile)

{

/ / extract the file to the specified directory

ZipHelper.ZipHelper.UnZipFile (zipfile, destpath, true)

/ / modify local version information after success

UpdateVersion (versioncode)

/ / restart the source program

If (appfile! = "")

{

StartApp (appfile)

}

}

After downloading the event, first extract the ZIP replacement file

Then modify the local version number information

Finally, restart the original program.

Thank you for reading! This is the end of this article on "how to achieve Winform automatic upgrade program in C#". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report