Developers, especially those who develop the client (C/S) system, will encounter a headache: automatic software updates; how to automatically update programs after the system is released, I have the honor to develop an automatic update program. The update program is completely independent from any host program. You only need to start the update program in the main program. The update program is also an executable file, during startup, you can set whether the update is automatic or manual. Automatic update means that you do not need manual intervention to download the update package from the remote server, for Manual updates, you can click the button in the program to implement updates. in Automatic Updates and Manual updates, you can select a project based on your needs, some programs must be updated before they can be used. Therefore, automatic update is necessary. Manual Update means that users can update programs at any time without strict version control; I will discuss the specific implementation details below. I have posted some code, and the source code is not suitable for uploading the company's property;
The purpose of automatic update is to copy the DLL file on the server to the local execution directory and overwrite the local file with the same name. The process is simple, but there are several points to note:
1. how can a large number of DLL files be downloaded to the local device? When multiple DLL files are downloaded slowly, packet loss or file loss may occur; I implemented multiple files through ICSharpCode. the SharpZipLib component can be packaged to save a lot of trouble. (For example, the dll name of the dynamic connection library may change in lower case during transmission)
2. after downloading to a local file, how to overwrite the original file with the same name? My implementation is to delete the file with the same name and decompress it. In this process, you need to temporarily Save the deleted file, to prevent failed operations from being started, pay attention to the transactional principle;
3. if the updated file is not just a DLL file, there may be some infinitus folders. In my own implementation, if there is a folder with the same name, delete it recursively and decompress it to the directory; because the top-level directory after the compressed package is decompressed is the name of the compressed file, you must pay attention to the directory hierarchy during the copy process;
Next let's take a look at the entire implementation process. Although the entire source code is not provided, it's okay if you finish reading this article;
For ease of deployment, I suggest you use a tool that implements a deployment file. You can package all the files and generate the server version information file;
This tool allows you to easily compress files, generate HASH values, version files, update addresses, and other information;
In this XML file, the current version information of the service, the name of the updated file, and the HASH value of the updated file are saved. Why does HASH need to be used to ensure that the updated file will be transferred in some situations, if all clients have serious consequences after update, we must include the HASH value;
The tool generates two files. One is the version file, the other is the update package, and the server task has been completed. The following describes the implementation of the specific client;
To know when version updates are required, you must save a file in the client program directory to record version information;
The file contains the current local version number, the server update address, and the host program name. You need the host name to enumerate the Host Program during the update process and then turn it off, in this way, the update process is not affected. Of course, the update process can be implemented without the Host Program being closed. If you use a host program that has been occupied by the Host Program, the update process will be directly affected, so it would be nice if you just shut it down;
This is the information stored in the Client Version file;
As we mentioned above, updates are divided into manual and automatic. Let's talk about Manual updates first. Manual updates require users to click the update button and start the update, this problem can be solved by passing process parameters;
Of course, this logical judgment is required in the update program;
The portal is used to determine the update method. Here, the remote update package is downloaded using a WebClient object or other Socket-based objects; before the update starts, check whether the local version number is earlier than the remote version number. If the local version number is earlier than the remote version number, update the local version number;
Because the download process is asynchronous, we recommend that you use System as the background thread. componentModel. backgroundWorker is a background Thread object. It encapsulates the Thread well. The following describes the Core Process Code:
Html # expandSource "commandName =" expandSource "highlighterId =" highlighter_26353 "> show sourceview sourceprint?
01 |
// Start the auxiliary thread operation |
02 |
private void Back_thread_DoWork( object sender, DoWorkEventArgs e) |
06 |
// Instantiate the download object |
07 |
downclient = new WebClient(); |
08 |
downclient.DownloadProgressChanged += new |