C # automatic upgrade (source code included)

Source: Internet
Author: User

C # automatic upgrade (source code included)

For PC desktop applications, automatic upgrade is often essential. The automatic upgrade can be developed as an independent C/S system, so that it can be reused in different desktop applications. This article focuses on the background and usage of us. For detailed implementation details, you can directly download the source code. If you understand the use of OAUS, it is very easy to understand the source code. If you need to directly deploy and use the automatic upgrade system, you can download the executable program compressed package at the end of this article.

I. Main Functions of OAUS

Currently, the mainstream automatic program upgrade policy is to re-download the latest installation package and then reinstall the entire client. This method is simple and intuitive, but its defects are also obvious. For example, even if the entire client has 100 MB, and this update only modifies a 1 K dll, it also means to download all the content of MB. This is a great waste of bandwidth, and it has extended the upgrade time, and correspondingly increased the waiting time of the customer.

In the above scenario, can we update only the modified 1 K dll during automatic upgrade? Of course, it is easy to use the automatic upgrade System of us. The automatic upgrade System of us can manage versions of each file in the distributed client program. The basic unit for each upgrade is not the entire client program, but a single file. Update a single file in three forms:

(1) The file is modified.

(2) The file is deleted.

(3) Add a new file.

OAUS supports these three forms of file updates. Each automatic upgrade can change N files, delete M files, and add new L files.

Ii. Use of OAUS 1. Structure of OAUS

OAUS provides directly executable server programs and client programs: AutoUpdaterSystem.Server.exe and AutoUpdater.exe. The directory structure of the OAUS server is as follows:

Through tcpcommunication between the client and the server, you can configure the TCP port through which the server provides the automatic upgrade service in the autoupdatersystem.server.exe. config configuration file.

The FileFolder folder is initially empty and is used to deploy the latest versions of each file of the program to be distributed. Note that the file structure under it must be exactly the same as that of the program to be distributed after normal deployment-that is, it is equivalent to deploying a program to be distributed under the FileFolder folder.

The directory structure of the OAUS client is as follows:

You can configure the IP address and port of the US us server in the autoupdater.exe. config configuration file, as shown in the following figure:

<Configuration> <etettings> <! -- {Cke_protected} {C} % 3C! % 2D % 2D % E6 % 9C % 8D % E5 % 8A % A1 % E5 % 99% A8IP % 20% 2D % 2D % 3E --> <add key = "ServerIP" value =" 127.0.0.1 "> <! -- {Cke_protected} {C} % 3C! % 2D % 2D % E6 % 9C % 8D % E5 % 8A % A1 % E5 % 99% A8 % E7 % AB % AF % E5 % 8F % A3 % 2D % 2D % 3E --> <add key = "ServerPort" value = "4530"> <! -- {Cke_protected} {C} % 3C! % 2D % 2D % E5 % 8D % 87% E7 % BA % A7 % E5 % AE % 8C % E6 % 88% E5 % 90% 8E % EF % BC % 8C % E5 % B0 % 86% E8 % A2 % AB % E5 % 9B % 9E % E8 % B0 % 83% E7 % 9A % 84% E5 % 8F % AF % E6 % 89% A7 % E8 % A1 % 8C % E7 % A8 % 8B % E5 % BA % 8F % E7 % 9A % 84% E5 % 90% 8D % E7 % A7 % B0 % 2D % 2D % 3E --> <add key = "CallbackExeName" value = "Demo.exe"> <! -- {Cke_protected} {C} % 3C! % 2D % 2D % E4 % B8 % BB % E7 % AA % 97% E4 % BD % 93% E7 % 9A % 84 Title % 2D % 2D % 3E --> <add key =" title "value =" file update "> </add> </appsettings> </configuration>

Pay attention to the configured CallbackExeName, which indicates the name of the exe of the dispatching program to be started after the upgrade is complete. Why is the CallbackExeName configured by name instead of path? This is because there are requirements for using and deploying the OAUS client:

(1) the executable file exe of the program to be distributed must be located in the root directory of the deployment directory.

(2) The client of OAUS (that is, the entire AutoUpdater folder) must also be located in this root directory.

In this way, AutoUpdater knows the relative path of the distribution program's exe, so that it can determine the absolute path of the distribution program's exe, so you can start the target exe after the upgrade is complete. In addition, according to the above two conventions, combined with the structure conventions of the FileFolder folder of the server mentioned above, when the server updates a file, autoUpdater can determine the absolute path of the file on the client machine.

2. Automatic Upgrade process for us

The following describes how to use OAUS to build an automatic upgrade system. The steps are as follows.

(1) run the OAUS server.

(2) Place all the content of the client program to the FileFolder folder of the OAUS server. The structure of the client program should be exactly the same as that of the client program after normal deployment. We deploy VideoChatSystem as an example.

(3) use the OAUS server to generate a default version number for each file of the distributed client program, and create the UpdateConfiguration. xml version configuration file. This configuration file will also be used by the client.

Click the "file version management" button on the server interface to bring up the "file version information" form for managing each file version.

   

After overwriting an old file with a new version, click the "auto scan" button to retrieve the name, size, and last update time of the file in the FileFolder folder, then the update result is obtained: several files have been changed, several files have been added, and several files have been deleted. The automatic scan function simplifies all previous manual operations.

If you need manual operations, you can do this:

Double-click any row in the list to modify the version of the corresponding file (an int value ). Note: The version information in this list and the actual version attributes of the file (for example, the dll version attribute X. x. x. x) It can be unrelated. The version value in the list is only used to mark whether the file is modified. Therefore, each time the file is modified, the value of the corresponding version in the list should be increased.

When the [file version information] form is closed, the value (int type) of the "Last integrated version" increases by 1 as long as a file version changes. Compare the value of the "final integrated version" Saved by the client of OAUS with the value of the latest "final integrated version" of the OAUS server, you can quickly identify whether the client is the latest version.

In addition, when this window is opened for the first time, a version configuration file UpdateConfiguration. xml is automatically generated under the directory of the OAUS server. In addition, every time you use this form to set a new version of a file, UpdateConfiguration. xml is automatically updated at the same time.

(4) add UpdateConfiguration. xml to the client program (the folder of the AutoUpdater mentioned above) of OAUS.

(5) When creating the installation program of the client to be distributed, package the client of OAUS (that is, the folder of AutoUpdater) and, as mentioned earlier, you need to deploy it directly under the running directory (BaseDirectory) (the same directory as the distribution exe ).

In this way, the preparation is complete. After the client installs VideoChatSystem through the installation package, its directory structure is as follows:

(6) When we have a new version to be released, such as updating a file (because the file is modified), we can do this:

A. Copy the modified file to the correct location (or overwrite the old file) in the FileFolder folder of the OAUS server ).

B. Open the file version information form on the OAUS server, double-click the Row corresponding to the modified file, modify the version number of the corresponding file on the pop-up form, and increase the value of the version number. (If you want to delete an old file or add a new file, you can also perform operations here)

(7)if this is the case, when the client starts autoupdater.exe again, it will automatically upgrade and update those files that have changed. In the following example, autoupdater.exe is run.

(8) after the upgrade is complete, the callback exe configured in the preceding OAUS client configuration file will be started. (Videochatsystem.exe in this example)

(9)usus the client will log on to the log file updatelog.txt (located in the AutoUpdater folder and automatically generate the file when the OAUS client is running for the first time) and record the status of each automatic upgrade.

(10) If the connection to the server is interrupted during the upgrade, the system will automatically reconnect. After the reconnection is successful, the resumable data transfer will be started.

3. When will automatic client upgrade be started?

If a system downloads a client, how does the client know whether a new version is available? Then why should I start autoupdater.exe?

Our experience is as follows: after successfully logging on to the client, obtain the value of "final comprehensive version" from the server and compare it with the value of the local "final comprehensive version, if the local value is small, the client needs to be updated. This process can be achieved as follows:

(1) When a new file is placed in the FileFolder folder of the OAUS server, and the version number is correctly updated through the [file version information] form, when you close the [file version information] form, the value of "last integrated version" is automatically added with 1.

(2) The System Client can determine whether a new version is available by calling the static method HasNewVersion () of the OAUS. Core. VersionHelper class.

(3) If the HasNewVersion method returns true, there are two modes: whether to upgrade or force upgrade.

Generally, if the latest client program is compatible with the old version and does not affect the upgrade, you can decide whether to upgrade it. If the latest client program is not compatible with the old version or has a major update, the Force upgrade will be started. If the process is to be imported into the startup upgrade, you only need to start autoupdater.exe in the file folder of autoupdater. To disable the current client process, after the autoupdater.exe process is started, some files may fail to be overwritten. The code is roughly as follows:

If (VersionHelper. hasNewVersion (oausServerIP, oausServerPort) {string updateExePath = AppDomain. currentDomain. baseDirectory + "AutoUpdater \ AutoUpdater.exe"; System. diagnostics. process myProcess = System. diagnostics. process. start (updateExePath );...... // exit the current process}

Related Article

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.