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> <Descr resume ption> fixed some bugs </descr resume ption> <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 (descr resume ption = "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 (descr update ption = "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 = "inDownload..."; 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. Iv. Summary: From this example, the work of WebService is very simple and easy to implement. The good use of WebService can bring many new and strong functions to our programs. All in all,. NET is an easy-to-use and powerful language. |