Use Web Services in WinForm to implement software automatic upgrade (C # C #

xiaoxiao2021-03-19  201

WINFORM programs are more powerful, more programming, but software updates are quite troublesome, to go to one place to upgrade, in the face of this actual problem, in a recent small project, I designed it A automatic upgrade technology program is achieved by software, making up for this lack, there is a good reference value. First, the benefits of upgrading. For a long time, the majority of programmers use client / server, or use Browser / Server structural arguments, in these debates, the C / S structure has poor maintenanceability, difficulty in layout, inconvenient upgrade, maintenance cost is high A quite important factor, is also an important reason for those B / S supporters to interact with the Client / Server structure into hell. Now, we use WebServices to implement software automatic upgrade features on the latest Microsoft WinForm. Second, the technical principle of the upgrade. There are several principles of the upgrade, first of all, comparing existing versions with the latest version, discovering the latest, prompting the user to upgrade. Of course, some people also compare them with other properties, for example: file size. :) or update the date. What about the implementation? In the VB era, I am using the XMLHTTP INET control. Use XMLHTTP to get information, transfer to upgrade files with an Inet, and use a simple BAT file to implement the upgrade. Public Sub Checkupdate ()

ON Error ResMe 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 VS

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 thenb = true

Elseif Revision> App.Revision Then

B = True

Else

B = FALSE

END IF

IF (b) THEN

Dim Result As VBMSGBoxResult

Result = msgbox ("Discover the new version of the program. The current version is:" & app.major & "." & app.minor & "." & App.Revision & ", the latest version is:" & Version & ", is it Do update? ", 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, which can delete yourself itself. Here is the content of the BAT file. @ Echo off

echo

Echo Echo welcomes the Upgrade Wizard of the Unreasonable Miracle Manager.

Echo This upgrade version is: 1.1.0.

Echo Please press any key to start upgrading the no-miracle manager ... Echo

echo

PAUSE

Del SQLSRVBROWSER.EXE

Ren ~ update.tmp sqlsrvbrowser.exe

Echo upgrade is successful, press any key to restart the application.

PAUSE

START http://mu.5inet.net/

START SQLSRVBROWSER.EXE

Del Update.bat

Third, the realization of the .NET era. In the .NET era, we have more choices, you can use WebRequest, you can also use WebServices. Here we will use WebServices to implement software automatic upgrade. Implementation Principle: Implement a GETVER webMethod method in WebServices, which is to get the current latest version. Then compare the current version with the latest version, if there is a new version, upgrade. Steps: 1, prepare an XML file (Update.xml). 1.0.1818.42821 Fix some bug The role is a template for an upgrade. 2, the Getver method for WebServices. [WebMethod (description = "Update version")] Public String GetVer ()

{

XmLDocument Doc = New XmLDocument ();

Doc.Load (Server.MAppath ("Update.xml");

Xmlelement root = doc.documentelement;

Return root.selectsinglenode ("version"). innerText;

} 3, WebServices's getUpdatedata method. [WebMethod (Description = "Online Update Software")]

[SoapHeader ("Sheader")]]]

Public system.xml.xmldocument getUpdatedata ()

{

/ / Verify that the user logs in

IF (Sheader == Null)

Return NULL;

IF (! DataProvider.getInstance.checklogin (Sheader.username, Sheader.Password))

Return NULL;

/ / Get updated XML template content

XmLDocument Doc = New XmLDocument ();

Doc.Load (Server.MAppath ("Update.xml"); XMLELEMENT ROOT = Doc.documenTelement;

// Take a look at there are several files that need to be updated.

XMLNode Updatenode = root.selectsinglenode ("filelist");

String path = Updatenode.attributes ["sourcepath"]. Value;

INT count = int.parse (Updatenode.attributes ["count"]. value);

// Replace Value in XML with actual content

For (int i = 0; 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);

/ / This is the actual content of the file, using Base64String encoding

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. First reference this webservices, for example named: WebsVS, String NVER = Start.getService.getVer (); if (Application.ProductVersion.comPareto (NVER) <= 0) Update (); in this code Start.getService is WebSVS A Static instance. First check the version, compare the results with the current version, if you execute the Update method for the new version. void update ()

{

this.statusbarpanel1.text = "Download ...";

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 ();

} Here is the simplicity of simplicity, there is no use of asynchronous methods, of course, use asynchronous methods to better improve customer experience, this need readers need to add themselves. :) Update's role is to download the upgrade XML file and save it as an Update.xml file in the execution file directory. The task is complete, exit the program, wait for Update.exe to upgrade.

5, the contents of Update.exe.

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

{

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 = "is being updated:" filename "(" itemnode.attributes ["size"]. value ") ..."

FileStream fs = file.open (filename, filemode.create, fileaccess.write);

Fs.write (ItemNode.Selectsinglenode ("Value"). InnerText), 0, INT.PARS (ItemNode.attributes ["size"]. Value);

fs.close ();

}

Label1.text = "Update Completion";

File.delete (Application.Startuppath @ "/ update.xml");

Label1.Text = "Restart the application ...";

System.Diagnostics.Process.start ("Customerapplication.exe");

CLOSE ();

Application.exit ();

}

This code is also easy to understand, first find the main process, if not closed, use process.kill () to close the main program. Then use an xmldocument to the Update.xml file generated by the Load program. Use the path and file name specified in the XML file to generate the specified file, which previously existing files deleted. After the update is completed, restart the primary application. This is done in this update. Fourth, summary: From this example, WebService work is very simple and it is easy to implement. A good use of WebService can bring a lot of new, strong features for our procedures. All in all, .NET is easy to use, powerful languages. If you have any opinions on this code, welcome to "Developer" Forum: http://forums.coder.cn/, hope to discuss with you. This article is also published on the blog: http://blogs.coder.cn/skyover/archive/2004/06/07/485.aspx

转载请注明原文地址:https://www.9cbs.com/read-130293.html

New Post(0)