How to Design winform update programs

Source: Internet
Author: User

Windows applications generally have an automatic update function, which requires a separate update program to update the main program. How can the main program detect updates, and how to update the main program? Next we will separate the research and analysis.

 

The installation program is released in the vs release wizard, and the entire installation program is downloaded and re-installed every time, sometimes the installation program is too large and the update is relatively ordinary (for example, we usually need to upgrade the module in the ERP system). to upgrade a separate module, the whole application will be upgraded, which will seriously affect the user experience. So we downloaded the remote module DLL, and the client used an XML file to save the version information. The server version information was saved in the database. Each time a user runs the client main program, the user checks whether the client version is consistent with the server version. if the version is inconsistent, the user must start the update program to update the module. Here, we will explain why we need to update the main program. Because we first run the main program, the main program cannot update itself during the running process, because the main program file is in use, it cannot be overwritten by remotely downloaded files, so you cannot write the update code logic in the main program to update yourself. So how can we solve this problem? Here is a little trick. Let's take a look at this at the main program intersection:Using system; <br/> using system. collections. generic; <br/> using system. windows. forms; </P> <p> using mainform; // DLL of the main program form <br/> using system. diagnostics; </P> <p> namespace windowsapplication1 <br/>{< br/> static class Program <br/>{< br/> // <summary> <br/> /// main entry point of the application. <Br/> // </Summary> <br/> [stathread] <br/> static void main (string [] ARGs) // note that parameters are added to my entry point. Main () is used for normal application creation () <br/>{< br/> // checks whether the master program is started by the update program. <br/> If (ARGs. length> 0 & ARGs [0] = "updated") <br/> {<br/> // If yes, run the main program <br/> application. enablevisualstyles (); <br/> application. setcompatibletextrenderingdefault (false); </P> <p> application. run (New xtraform3 ()); // xtraform3 is the main program form </P> <p >}< br/> else <br/>{< br/> // if not, run the update program to check whether update is required. <br/> Process = new process (); <br/> process. startinfo. filename = application. startuppath + "// yexin2.0update.exe"; // the location where the update program is located <br/> process. start (); // start the Update Program <br/> application. exit (); // close the main program <br/>}</P> <p >}< br/>}, We can see that if it is not the main program started by the update program (the update program starts the main program as described below), it will start the update program and close itself, let the update program update the main program or run the main program. In this way, the main program is not running. You can download the server's main program DLL to overwrite the local main program DLL to update the main program.

 

There should be a lot of updated program logic on the Internet. I will briefly describe my logic. The version file has a total version number, and then the version number of each module. If the total version number is the same, it indicates that the current program is the latest. the version number of each module is not checked. Otherwise, the system checks the version number of each module to determine whether to update the module.

 

After the update is successful, we need to synchronize the version information of the local file to the module DLL and start the main program. After the update is successful, we can write as follows:Copyfilesdirs (basesavepath, application. startuppath); // overwrite the downloaded file to a local file </P> <p> directory. delete (basesavepath, true); // delete a local Temporary Folder </P> <p> updateversionfile (); // update the local version File Information </P> <p> If (MessageBox. show ("Update successful! Start the ERP program? "," Update prompt ", messageboxbuttons. yesno, messageboxicon. information) = dialogresult. yes) <br/>{</P> <p> If (file. exists (application. startuppath + "// yexinerp2winform.exe") <br/>{< br/> // start the main program <br/> Process = new process (); <br/> process. startinfo. filename = application. startuppath + "// yexinerp2winform.exe"; <br/> process. startinfo. arguments = "updated"; // transfer the startup parameter, which is important here. You need to determine this parameter at the entry point of the main program to determine whether it is a new process. <Br/> process. start (); </P> <p >}< br/> else <br/>{< br/> MessageBox. show ("No ERP running program found! "); <Br/>}< br/>}Here, I will save the files to be updated downloaded from the server under a temporary folder, instead of downloading them to overwrite them. After all the files are downloaded, I will overwrite the local files, delete A Temporary Folder. Why? Because sometimes the download may be terminated during the download process (the user clicks the cancel Download button to terminate the download process or terminates the process of updating the program abnormally, for example, killing the process directly ), if the local file is downloaded and written directly to the local file, the local file may be damaged (A 1 mb dll file is interrupted when the download is half done, this DLL file is damaged ).

 

Under normal circumstances, no changes will be made after the update program is completed, so I do not need to update myself, so I have to update myself. How can this problem be solved? You can only make the update program an installer. Use the update program to download and update the installer. After the download, close the update program and run the update installer to update yourself.

 

 

Well, the above are some of the little experiences I have summarized during auto update, hoping to help readers.

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.