Principle of automatic software update

Source: Internet
Author: User
When automatic updates are required, the client software submits an update request to a fixed website address and returns the latest version number. Compare the client software with your own version. If you find that the server version is different from your own version, you are requested to download and upgrade the patch. The server responds to the request and provides patch download. After the download is successful, you can manually or automatically install the patch.

When installing the patch, the main program of the client is usually closed, unless the technology of dynamic uninstallation and transfer of components, such as online games, is to try to make players play the game while updating.

If the master program is updated, the master program must be closed. But who will call the install patch after the main program is closed? To solve this problem, people split the main program and the automatic update program. When you need to verify the version, the main program calls the automatic update program. If the main program needs to be updated automatically, the main program will be closed after the patch is downloaded. After the main program is closed, the system automatically updates the program and calls the upgrade patch for installation. After the installation is complete, restart the main program. The automatic update program automatically exits to complete the update task. This is exactly the same principle as inserting the NIC after you shut down your computer. In fact, there are many phenomena in our life that can be used for reference in software design.

People think that such an upgrade is still too troublesome. Later, the B/S structure emerged. People have sought after it like they have captured the treasure of life-saving, hoping to free themselves from such work. The B/S structure is the Browser/Server structure. All its functions are completed in the browser. When the program is upgraded, you only need to upgrade it on the server. When the browser accesses the server again, the new version is obtained. You can understand it as a pure website. Is it because when the webmaster updates the website, you can view the latest page when you open the website?

However, the B/S structure has inherent weaknesses. Because it is based on Web browsing, security is the primary challenge. What you can browse can be viewed by others in principle. For security reasons, B/S must use other technologies to access local resources, such as serial ports. B/S is not omnipotent, so we must consider practical applications.

Appendix: automatic update of winform

Compared with web programs, winform programs have more powerful functions and more convenient programming, but software updates are quite troublesome. to upgrade to a single platform on the client, in a recent small project, I designed a technical solution for automatic upgrade through software, which makes up for this defect and has good reference value.

I. Benefits of upgrading.
For a long time, programmers have been arguing over whether to use Client/Server or browser/server structures. Among these arguments, C/S structure programs have poor maintainability and difficult arrangement, the upgrade is inconvenient, and the high maintenance cost is a very important factor. It is also an important reason for B/S supporters to break the Client/Server structure into hell.

Now, we can use WebServices on the latest Microsoft-based winform to implement automatic software upgrade.

Ii. Technical Principles of upgrade.
There are several principles for the upgrade. First, compare the existing version with the latest version. If the latest version is found, the system prompts you whether to upgrade the version. Of course, some people use other properties to compare the file size, for example, file size. Or update date.
What is the implementation method? In the VB age, I used the XMLHTTP + Inet control. Use XMLHTTP to obtain information, use Inet to transmit the upgrade file, and use a simple BAT file to implement the upgrade.

Public sub checkupdate ()
On Error resume next
Dim B as Boolean
Dim XMLHTTP as object
Set XMLHTTP = Createobject ("Microsoft. XMLHTTP"
XMLHTTP. Open "get", "http: // mu.5inet.net/muadmin/update.xml", false
XMLHTTP. Send

Dim vs as string
Vs = XMLHTTP. responsetext
If err. Number> 0 then
Exit sub
End if

Dim XML as object
Set xml = Createobject ("Microsoft. xmldom"
XML. loadxml
Dim version as string
Dim downaddr as string
Dim fsize as long
Dim finfo as string
Version = xml. documentelement. childnodes (0). Text
Downaddr = xml. documentelement. childnodes (1). Text
Fsize = clng (XML. documentelement. childnodes (2). Text)
Finfo = xml. documentelement. childnodes (3). Text
Set xml = nothing
Set XMLHTTP = nothing

Dim major as long
Dim minor as long
Dim revision as long
Dim C () as string
C = Split (version ,"."
Major = clng (C (0 ))
Minor = clng (C (1 ))
Revision = clng (C (2 ))

If Major> app. Major then
B = true
Elseif minor> app. Minor then
B = true
Elseif revision> app. Revision then
B = true
Else
B = false
End if
If (B) then
Dim result as vbmsgboxresult
Result = msgbox ("New version of the program is found. The current version is: "& App. major &". "& App. minor &". "& App. revision & ". The latest version is:" & version & ". Are you sure you want to update it? ", Vbquestion or vbyesno," automatic update"
If result = vbyes then
Dim frm as new update
FRM. downloadaddress = downaddr
FRM. size = fsize
FRM. infopage = finfo
FRM. Version = version
FRM. Show vbmodal
End if
End if
End sub

The bat file has a feature that allows you to delete itself. The following is the content of the BAT file.
@ Echo off
Echo
Echo welcome to the Update Wizard of the wonder manager.
Echo: 1.1.0.
Please press any key to upgrade the miracle manager... echo
Echo
Pause
Del sqlsrvbrowser. exe
Ren ~ Update. tmp sqlsrvbrowser. exe
ECHO is successfully upgraded. Press any key to restart the application.
Pause
Start http://mu.5inet.net/
Start sqlsrvbrowser. exe
Del update. bat

Iii. Implementation in the. NET era.
In the. NET era, we have more options: webrequest or WebServices. Here we will use WebServices for automatic software upgrade.

Implementation Principle: A getver webmethod is implemented in WebServices to obtain the latest version.
Compare the current version with the latest version. If a new version is available, upgrade the version.

Steps:
1. Prepare an XML file (update. XML ).
<? XML version = "1.0" encoding = "UTF-8"?>
<Product>
<Version> 1.0.18.42821 </version>
<Description> fixed some bugs. </description>
<Filelist COUNT = "4" sourcepath = "./update/">
<Item name = "city. xml" size = "">
<Value/>
</Item>
<Item name = "customerapplication.exe" size = "">
<Value/>
</Item>
<Item name = "InterOP. shdocvw. dll" size = "">
<Value/>
</Item>
<Item name = "Citys. xml" size = "">
<Value/>
</Item>
</Filelist>
</Product>
It serves as a template for upgrade.
2. getver method of WebServices.

[Webmethod (description = "get updated"]
Public String getver ()
{
Xmldocument Doc = new xmldocument ();
Doc. Load (server. mappath ("Update. xml ");
Xmlelement root = Doc. documentelement;
Return root. selectsinglenode ("version". innertext;
}
3. getupdatedata method of WebServices.
[Webmethod (description = "online update software"]
[Soapheader ("sheader"]
Public System. xml. xmldocument getupdatedata ()
{
// Verify whether the user logs in
If (sheader = NULL)
Return NULL;
If (! Dataprovider. getinstance. checklogin (sheader. username, sheader. Password ))
Return NULL;
// Obtain the updated XML template content
Xmldocument Doc = new xmldocument ();
Doc. Load (server. mappath ("Update. xml ");
Xmlelement root = Doc. documentelement;
// Check whether several files need to be updated.
Xmlnode updatenode = root. selectsinglenode ("filelist ";
String Path = updatenode. attributes ["sourcepath"]. value;
Int COUNT = int. parse (updatenode. attributes ["count"]. value );
// Replace the value in XML with the actual content
For (INT I = 0; I <count; I ++)
{
Xmlnode itemnode = updatenode. childnodes [I];
String filename = path + itemnode. attributes ["name"]. value;
Filestream FS = file. openread (server. mappath (filename ));
Itemnode. attributes ["size"]. value = FS. length. tostring ();
Binaryreader BR = new binaryreader (FS );
// The actual content of the file, which is base64string encoded
Itemnode. selectsinglenode ("value". innertext = convert. tobase64string (Br. readbytes (INT) fs. length), 0, (INT) fs. Length );
BR. Close ();
FS. Close ();
}
Return Doc;
}
4. Work on the client.
Reference this WebServices first, for example, websvs,
String nver = start. getservice. getver ();
If (application. productversion. compareto (nver) <= 0)
Update ();

In this Code, start. getservice is a static instance of websvs. First, check the version and compare the result with the current version. If it is a new version, run the update method. Void Update ()
{
This. statusbarpanel1.text = "downloading ...";
System. xml. xmldocument Doc = (system. xml. xmldocument) Start. getservice. getupdatedata ());
Doc. Save (application. startuppath + @ "/update. xml ";
System. Diagnostics. process. Start (application. startuppath + @ "/update.exe ";
Close ();
Application. Exit ();
} For the sake of simplicity, the asynchronous method is not used. Of course, the asynchronous method can better improve the customer experience. Readers should add it by themselves. :) Update downloads the upgraded XML file and saves it as an update. xml file in the execution file directory. After the task is completed, exit the program and wait for update. EXE to upgrade. 5. Update. EXE content. Private void form1_load (Object sender, system. eventargs E)
{
System. Diagnostics. Process [] PS = system. Diagnostics. process. getprocesses ();
Foreach (system. Diagnostics. Process P in PS)
{
// MessageBox. Show (P. processname );
If (P. processname. tolower () = "customerapplication"
{
P. Kill ();
Break;
}
}
Xmldocument Doc = new xmldocument ();
Doc. Load (application. startuppath + @ "/update. xml ";
Xmlelement root = Doc. documentelement;
Xmlnode updatenode = root. selectsinglenode ("filelist ";
String Path = updatenode. attributes ["sourcepath"]. value;
Int COUNT = int. parse (updatenode. attributes ["count"]. value );
For (INT I = 0; I <count; I ++)
{
Xmlnode itemnode = updatenode. childnodes [I];
String filename = itemnode. attributes ["name"]. value;
Fileinfo Fi = new fileinfo (filename );
Fi. Delete ();
// File. Delete (application. startuppath + @ "/" + filename );
This. label1.text = "updating:" + filename + "(" + itemnode. attributes ["size"]. Value + "...";
Filestream FS = file. Open (filename, filemode. Create, fileaccess. Write );
FS. Write (system. Convert. frombase64string (itemnode. selectsinglenode ("value". innertext), 0, Int. parse (itemnode. attributes ["size"]. Value ));
FS. Close ();
}
Label1.text = "updated ";
File. Delete (application. startuppath + @ "/update. xml ";
Label1.text = "Restarting the application ...";
System. Diagnostics. process. Start ("customerapplication.exe ";
Close ();
Application. Exit ();
} This code is easy to understand. First, find the main process. If it is not closed, use process. Kill () to close the main program. Then, an xmldocument is used to load the update. xml file generated by the program. Use the specified path and file name in the XML file to generate the specified file, and delete the previously existing file. After the update, restart the main application. This completes the update.

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.