Auto Update component sharing
This article from: http://www.cnblogs.com/KnightsWarrior/archive/2010/10/20/1856255.html
I. Summary
An automatic update component in English was published on the blog two days ago.ArticleRelease a autoupdater tool. In this article, we also briefly describe its functions. This component is very simple, so you can download and make some improvements. As we all know, for general software development, there will be a technical selection stage at the beginning. The biggest selection is to first determine whether to select the Client/Server mode or browser/server mode. To sum up, the two have their own advantages and disadvantages and cannot be replaced by each other in many aspects. For example, B/S is much better than c/s in terms of Internet application and maintenance workload; however, B/S is far less powerful than c/s in terms of operation speed, data security, and human-computer interaction. Therefore, all c/s strengths are weaknesses of B/S, and vice versa. As we are talking about the automatic update of components today, let's talk about this in detail. Since the C/S mode has so many advantages in terms of running speed, data security, and human-machine interaction, in particular, with the increasing development of client technology, it is very important to solve the problems of client deployment and automatic upgrade.
Ii. Outline of this Article
· 1. Summary
· 2. Outline of this Article
· 3. Why not use clickonce?
· 4. Brief Introduction
· 5. How to use it in the project
· 6. Specific Results
· 7. Maintenance and download
· 8. Summary
Iii. Why not use clickonce
In the previous summary, we briefly introduced the importance of the automatic update function. In this section, let's talk about why we didn't use the automatic update component clickonce provided by Microsoft, we all know that clickonce provides us with many features: Simply put, clickonce applicationsProgramIs any windows form or console application released using clickonce technology. You can publish a clickonce application in three different ways: publishing from a Web page, sharing from a network file, or publishing from a media such as a CD-ROM. Clickonce applications can be installed on the end user's computer and run locally (even when the computer is offline) or only in online mode, without permanently installing any content on the end user's computer. Clickonce applications can be updated by themselves. These applications can check the latest version when the new version is available and automatically replace all the updated files. Developers can specify the update behavior. The network administrator can also control the update policy, such as marking the update as mandatory. The end user or administrator can also roll back the updates to restore the application to an earlier version.
From the above we can see that clickonce is undoubtedly the best solution for Microsoft to deploy the Client/Server mode, but it is precisely because of its powerful functions and needs to be quite simple to use, therefore, the product encapsulation is very strict, and some simple operation interfaces are basically exposed, so that some customized operations are not rejected, such:
1. You cannot specify the installation path yourself.
2. You cannot customize the automatic update process.
3. The automatically updated UI cannot be customized.
For these reasons, many enterprises have customized components to implement automatic update. Based on this, we have also implemented a very simple automatic update component.
Iv. Brief Introduction
In fact, the principle of automatic update is very simple. The analysis is nothing more than a few simple steps. Of course, the implementation method is similar. Here we choose a simple method:
1. Start the main program, call the Upgrade Program in the main program, and connect the Upgrade Program to IIS or FTP.
2. the Upgrade Program obtains the update date, version number, or file size of the new version program in the XML configuration file on the server.
3. the upgrade program obtains the latest update date, version number, or file size of the original client application, and then compares the two. If the new version date is later than the latest date of the original program, the system prompts the user whether to upgrade; or, if the version number of the new version> the version number of the original program, the system prompts you whether to upgrade. or if the file size of the new version> the file size of the original program, the system prompts you whether to upgrade. This document uses the version number for comparison.
4. If the user chooses to upgrade, the downloaded file list will be obtained;
5. Create a local temporary directory corresponding to Remote IIS or FTP, and then download it to this temporary directory file;
6. Delete the old main program and copy the files in the Temporary Folder to the corresponding location;
8. End the upgrade process and restart the main program.
Based on the previous process, we can simply design the following projects:
Figure 1
Class introduction:
Iautoupdater. CS provides APIs for external calls
Autoupdater. CS main operation class of this component
Autoupdater. config local configuration file
Downloadconfirm. CS indicates whether an Update page exists.
Downloadprogress. CS download progress page
Commonunitity. CS common functions
Config. CS: The Config must be updated after the update. Therefore, a Config class that provides serialization is required.
Constfile. CS some constant files
Downloadfileinfo. CS object class to be downloaded
Localfile. CS local file entity class
Remotefile. CS Remote File entity class
Updatefilelist. CS local object class set
CodeIt is very simple. You can download and view it, so I will not elaborate on it too much here.
5. How to Use
Step 1: update the host version to the server
If you want the client to obtain the latest version, you must first compileSource codeGenerate the file, copy it to the FTP or IIS Directory, run a program that automatically generates the XML file, and automatically generate all the files to an XML file. For details, see:
Figure 2
Step 2: Configure local config
The first step is to configure the local config to monitor and download the files to be updated in Remote IIS or FTP, as shown in:
Figure 3
Step 3: Modify the main program
First, introduce the DLL autoupdater into our main project, and then add the following code to the main project. Of course, you can write it as needed. This dll provides two external interfaces, one interface is used to determine whether updates and downloads exist, and the other interface is used to perform rollback when an update error occurs. The specific code is as follows:
# Region check and downloadNew Version Program Bool Bhaserror = False ; Iautoupdater autoupdater = New Autoupdater (); Try {Autoupdater. Update ();} Catch (Webexception exp) {MessageBox. Show (" Can not find the specified resource "); Bhaserror = True ;} Catch (Xmlexception exp) {bhaserror = True ; MessageBox. Show (" Download the upgrade file Error ");} Catch (Notsupportedexception exp) {bhaserror = True ; MessageBox. Show (" Upgrade Address Configuration Error ");} Catch (Argumentexception exp) {bhaserror = True ; MessageBox. Show (" Download the upgrade file Error ");} Catch (Exception exp) {bhaserror = True ; MessageBox. Show (" An error occurred during the upgrade process ");} Finally { If (Bhaserror = True ){ Try {Autoupdater. rollback ();} Catch (Exception ){ // Log the message to your file or database }}# Endregion
The usage is so simple and more detailed. You can download the source code, and because of its simplicity, you can modify it to meet the needs of specific projects.
Vi. Specific Results
When we run the main program (winform or WPF), if the server has the latest version, the following page will pop up and prompt you to choose whether to update.
Figure 4
When you do not need updates, you can select the Skip button to skip and continue the main program process. Otherwise, the following page is displayed.
Figure 5
During the download process, you can select Cancel to stop the download and return to the main process.
7. Maintenance and download
This component has been hosted on codeplex, so you can download its source code above, the specific address: http://autoupdater.codeplex.com/
Figure 6
8. Summary
This article briefly describes a simple automatic update component. Because it is simple and has a single function, it does not take much space to discuss it. Interested readers can download its source code. Later, I will host a self-developed mvvm framework. As the current support for Silverlight is not very good, we are continuously making relevant improvements, I have borrowed some ideas from other excellent frameworks and integrated them into some practical application scenarios. So I hope you will be able to give me more advice!