Implement automatic upgrades in WinForm applications

Source: Internet
Author: User

This is my first time to write more complex articles, the expression is not clear, please forgive me. Well, gossip less, come to the point.

The most recent unit develops a project that requires the automatic upgrade feature. Because automatic upgrade is a more common function, may be used in many programs, so I want to write an automatic upgrade component, in the application, only need to reference this automatic upgrade components, and add a small amount of code to achieve automatic upgrade function. Because our program may contain multiple EXE or DLL files, we need to support multiple file updates.

First, determine where the program should go to download files that need to be upgraded. I chose to go to the designated website to download, this is simpler, but also more general. On this site, you need to place a file that currently describes the latest file list, which we estimate and call the server configuration file. This file holds the current version number (Lastver) of the latest file, the size, the download address (URL), the local file's save path (path), and whether the program needs to be restarted (Needrestart) when the file is updated. This document is roughly as follows:
Updateservice.xml;? XML version= "1.0" encoding= "Utf-8"?>
< Updatefiles >
< file Path = "AutoUpdater.dll" url = "Http://update.iyond.com/CompanyClientApplication/AutoUpdater.zip" lastver = "1 .0.0.0 "size = 28672" Needrestart = "true"/>
< file Path = "CompanyClient.exe" url = "Http://update.iyond.com/CompanyClientApplication/CompanyClient.zip" Lastver = "1.1.0.0" size = "888832" Needrestart = "true"/>
< file Path = "HappyFenClient.dll" url = "Http://update.iyond.com/CompanyClientApplication/HappyFenClient.zip" Lastver = "1.0.0.0" size = "24576" Needrestart = "true"/>
< file Path = "NetworkProvider.dll" url = "Http://update.iyond.com/CompanyClientApplication/NetworkProvider.zip" Lastver = "1.0.0.0" size = "32768" Needrestart = "true"/>
< file Path = "Utility.dll" url = "Http://update.iyond.com/CompanyClientApplication/Utility.zip" lastver = "1.0.0.0" Size = "20480" Needrestart = "true"/>
< file Path = "Wizard.dll" url = "Http://update.iyond.com/CompanyClientApplication/Wizard.zip" lastver = "1.0.0.0" si Ze = "24576" Needrestart = "true"/>
</updatefiles >

At the same time, the client also saves a list of local files that need to be upgraded, as in the form of a server configuration file, which we call a local profile. Where the,<enable> node indicates whether the automatic upgrade feature is enabled,<serverurl> represents the address of the server configuration file.
Update.config;? XML version= "1.0" encoding= "Utf-8"?>
< Config xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd = "Http://www.w3.org/2001/XMLSchema" >
< enabled > True </Enabled >
< ServerURL > Http://update.iyond.com/updateservice.xml </serverurl >
< Updatefilelist >
< LocalFile Path = "AutoUpdater.dll" lastver = "1.0.0.0" size = "28672"/>
< LocalFile Path = "CompanyClient.exe" lastver = "1.1.0.0" size = "888832"/>
< LocalFile Path = "HappyFenClient.dll" lastver = "1.0.0.0" size = "24576"/>
< LocalFile Path = "NetworkProvider.dll" lastver = "1.0.0.0" size = "32768"/>
< LocalFile Path = "Utility.dll" lastver = "1.0.0.0" size = "20480"/>
< LocalFile Path = "Wizard.dll" lastver = "1.0.0.0" size = "24576"/>
</updatefilelist >
</Config >

Programs that use automatic levels of components will check for this profile when they are started. If a file version in the profile is found to be inconsistent with the version of the file described in the local configuration file, the user is prompted to download it. Also, if some files in the local profile do not exist in the file list of the server configuration file, the file is no longer needed and needs to be removed. Finally, when the upgrade is complete, the local profile is updated.

Let's look at how to use this component first.
In the Program.cs main function of the program: [STAThread]
static void Main ()
{
Application.enablevisualstyles ();
Application.setcompatibletextrenderingdefault (FALSE);

Autoupdater au = new Autoupdater ();
Try
{
Au. Update ();
}
catch (WebException exp)
{
MessageBox.Show (String.Format ("Cannot find specified resource/n/n{0}", exp. Message), "Automatic Upgrades", MessageBoxButtons.OK, Messageboxicon.error);
}
catch (XmlException exp)
{
MessageBox.Show (String.Format ("Download upgrade file has error/n/n{0}", exp. Message), "Automatic Upgrades", MessageBoxButtons.OK, Messageboxicon.error);
}
catch (NotSupportedException exp)
{
MessageBox.Show (String.Format ("Upgrade address configuration Error/n/n{0}", exp. Message), "Automatic Upgrades", MessageBoxButtons.OK, Messageboxicon.error);
}
catch (ArgumentException exp)
{
MessageBox.Show (String.Format ("Download upgrade file has error/n/n{0}", exp. Message), "Automatic Upgrades", MessageBoxButtons.OK, Messageboxicon.error);
}
catch (Exception exp)
{
MessageBox.Show (String.Format ("Error/n/n{0} during upgrade)", exp. Message), "Automatic Upgrades", MessageBoxButtons.OK, Messageboxicon.error);
}

Application.Run (New Mainui ());
}


as shown above, with just a few lines of code, you can implement the automatic upgrade feature.

Software Run screenshots:






Below, let's go over the implementation of this automatic upgrade component.
Look at the class diagram first:

Autoupdater: An automatic upgraded management class that is responsible for the implementation of the overall automatic upgrade feature.
Config: The configuration class that is responsible for managing the local configuration file.
Downloadconfirm: A dialog box that shows the user a list of files that need to be upgraded and allows the user to choose whether to upgrade immediately.
Downloadfileinfo: Information for the file to download
DownloadProgress: A dialog box that shows the progress of the download.
Downloadprogress.exitcallback,
Downloadprogress.setprocessbarcallback,
Downloadprogress.showcurrentdownloadfilenamecallback: Because. NET2.0 does not allow access to the object of another thread in one thread, it needs to be implemented by a delegate.
LocalFile: Represents a file in the local profile
RemoteFile: Represents a file in the server configuration file.
Updatefilelist: A collection, inheriting from list<localfile>

Let's take a look at AutoUpdater.cs:AutoUpdater.cs
public  Class autoupdater
{
    const string FILENAME =  " Update.config ";
    private Config config = null;
    private bool bNeedRestart = false;

    public autoupdater ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.