WinformProgramCompared with web programs, it is more powerful and easier to program, but software updates are quite troublesome. to upgrade to a single platform on the client, in the face of this practical problem, in a recent small project, I have 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 () <br/> on error resume next <br/> dim B as Boolean <br/> dim XMLHTTP as object <br/> set XMLHTTP = Createobject ("Microsoft. XMLHTTP ") <br/> XMLHTTP. open "get", "http: // mu.5inet.net/muadmin/update.xml", false <br/> XMLHTTP. send <br/> dim vs as string <br/> Vs = XMLHTTP. responsetext <br/> If err. number> 0 then <br/> exit sub <br/> end if <br/> dim XML as object <br/> set xml = Createobject ("Microsoft. xmldom ") <br/> XML. loadxml vs <br/> dim version as string <br/> dim downaddr as string <br/> dim fsize as long <br/> dim finfo as string <br/> Version = XML. documentelement. childnodes (0 ). text <br/> downaddr = xml. documentelement. childnodes (1 ). text <br/> fsize = clng (XML. documentelement. childnodes (2 ). text) <br/> finfo = xml. documentelement. childnodes (3 ). text <br/> set xml = nothing <br/> set xmlht TP = nothing <br/> dim major as long <br/> dim minor as long <br/> dim revision as long <br/> dim C () as string <br/> C = Split (version ,". ") <br/> major = clng (C (0) <br/> minor = clng (C (1 )) <br/> revision = clng (C (2) <br/> if major> app. major then <br/> B = true <br/> elseif minor> app. minor then <br/> B = true <br/> elseif revision> app. revision then <br/> B = true <br/> else <br/> B = false <br/> end if <br /> If (B) Then <br/> dim result as vbmsgboxresult <br/> 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," Auto Update ") <br/> If result = vbyes then <br/> dim frm as new update <br/> frm. downloadaddress = downaddr <br/> frm. size = fsize <br/> frm. infopage = finfo <br/> frm. version = version <br/> frm. show vbmodal <br/> end if <br/> end sub <br/>
The bat file has a feature that allows you to delete itself. The following is the content of the BAT file.
@ Echo off <br/> echo <br/> Echo welcome to the innocent miracle Manager Upgrade Wizard. <Br/> ECHO: 1.1.0. <Br/> press any key to upgrade echo manager... echo <br/> pause <br/> Del sqlsrvbrowser. EXE <br/> Ren ~ Update. tmp sqlsrvbrowser. EXE <br/> ECHO is successfully upgraded. Press any key to restart the application. <Br/> pause <br/> Start http://mu.5inet.net/<br/> Start sqlsrvbrowser. EXE <br/> Del update. Bat <br/>
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.
step:
1. Prepare an XML file (update. XML ).
1.0.18.42821
fixed some bugs
serves as an upgrade template.
2. getver method of WebServices.
[Webmethod (description ="Get updated version")]Public StringGetver () {xmldocument Doc= NewXmldocument (); Doc. Load (server. mappath ("Update. xml"); Xmlelement Root=Doc. documentelement;ReturnRoot. 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 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, the update method is executed.
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 need to add it by themselves.
:) Update downloads the upgraded XML file and saves it as an update. xml file in the execution file directory. Task completed,
Exit the program and wait for update. EXE to upgrade.
5. Update. EXE content.
Private Void Formateload (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 also easy to understand. First, find the main process. If it is not closed, use process. Kill () to close the main program. Then use
Xmldocument 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.
Delete an 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. Make good use of WebService for us
The program brings many new and strong functions. All in all,. NET is an easy-to-use and powerful language. If you have any comments on this code, welcome
Visit the "Developer" Forum: http://forums.coder.cn/, hope to discuss with you.