Use Web Service in winform for automatic software upgrade

Source: Internet
Author: User

Source: Internet

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 the client/server or Browser/Server structure. Among these arguments, the maintainability of C/S structure programs is poor.Difficult arrangement and inconvenient upgrade. High maintenance cost is a very important factor, and B/S supporters put the Client/Server

An important reason for the structure to go to hell. Now, we can use web service 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 other attributes are compared, 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. The bat file has a feature that allows you to delete itself.

Iii. Implementation in the. NET Era

 In the. NET era, we have more options: webrequest or web service. Here we will use Web Service for automatic software upgrade. Implementation principle: implement a getver webmethod in the Web service to obtain the latest version. Compare the current version with the latest version. If a new version is available, upgrade the version. The procedure is as follows:

1. Prepare an XML file (update. XML) used as the upgrade template ).

<? 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>

2. The getver method of web service.

[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. The getupdatedata method of web service.

[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 web service 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 need to add it by themselves.
Update downloads the upgraded XML file and saves it as an update. xml file under 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 also 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 loadUpdate. xml file. 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 Web Service is very simple and easy to implement. The good use of web service can bring many new and strong functions to our programs. All in all,. NET is an easy-to-use and powerful language.

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.