asp.net (C #) Program version Upgrade Update implementation Code _ Practical Tips

Source: Internet
Author: User
Directly on the code:
Copy Code code as follows:

Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Reflection;
Using System.IO;
Using System.Net;
Using System.Xml;
Namespace Update
{
<summary>
Update completion of triggered events
</summary>
public delegate void Updatestate ();
<summary>
program updates
</summary>
public class Softupdate
{
private string Download;
Private Const string UpdateUrl = "Http://www.jb51.net/update.xml";//upgrade configuration's XML file address
#region Constructors
Public softupdate () {}
<summary>
program updates
</summary>
<param name= "File" > Files to be updated </param>
Public Softupdate (String file,string softname) {
This. LoadFile = file;
This. Softname = Softname;
}
#endregion
#region Properties
private string LoadFile;
private string Newverson;
private string Softname;
private bool Isupdate;
<summary>
or whether to update
</summary>
public bool Isupdate
{
Get
{
Checkupdate ();
return isupdate;
}
}
<summary>
To check for updated files
</summary>
public string LoadFile
{
get {return loadfile;}
set {loadfile = value;}
}
<summary>
New version of Assembly
</summary>
public string Newverson
{
get {return newverson;}
}
<summary>
Name of the upgrade
</summary>
public string Softname
{
get {return softname;}
set {softname = value;}
}
#endregion
<summary>
Events that are triggered when an update completes
</summary>
public event Updatestate Updatefinish;
private void Isfinish () {
if (updatefinish!= null)
Updatefinish ();
}
<summary>
Download updates
</summary>
public void Update ()
{
Try
{
if (!isupdate)
Return
WebClient WC = new WebClient ();
string filename = "";
string exten = download. Substring (download. LastIndexOf ("."));
if (Loadfile.indexof (@ "\") = = 1)
filename = "Update_" + path.getfilenamewithoutextension (loadfile) + exten;
Else
filename = Path.getdirectoryname (loadfile) + "\\Update_" + path.getfilenamewithoutextension (loadfile) + exten;
Wc. DownloadFile (download, filename);
Wc. Dispose ();
Isfinish ();
}
Catch
{
throw new Exception ("Update error, network connection failed!") ");
}
}
<summary>
Check if updates are required
</summary>
public void Checkupdate ()
{
try {
WebClient WC = new WebClient ();
Stream stream = WC. OpenRead (UpdateUrl);
XmlDocument xmldoc = new XmlDocument ();
Xmldoc.load (stream);
XmlNode list = Xmldoc.selectsinglenode ("Update");
foreach (XmlNode node in list) {
if (node. Name = = "Soft" && node. attributes["Name"]. Value.tolower () = = Softname.tolower ()) {
foreach (XmlNode XML in node) {
if (XML. Name = = "Verson")
Newverson = XML. InnerText;
Else
Download = XML. InnerText;
}
}
}
Version ver = new version (Newverson);
Version Verson = Assembly.LoadFrom (loadfile). GetName (). Version;
int TM = Verson.compareto (ver);
if (tm >= 0)
Isupdate = false;
Else
Isupdate = true;
}
catch (Exception ex) {
throw new Exception ("Update error, please confirm network connection is correct and try again!") ");
}
}
<summary>
Get the file to update
</summary>
<returns></returns>
public override string ToString ()
{
return this.loadfile;
}
}
}

Compiling the code into a class library file is OK by referencing the program.
The parameters passed in already have a comment.
The following is the updated XML file class capacity, uploaded to the space above it, get the address of the XML file.
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<Update>
<soft name= "Blogwriter" >
<Verson>1.0.1.2</Verson>
<DownLoad>http://www.jb51.net/BlogWrite.rar</DownLoad>
</Soft>
</Update>

Program Update call Method:
1. Reference the above DLL first.
2, the calling method code is as follows:
Copy Code code as follows:

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Text;
Using System.Windows.Forms;
Using System.IO;
Using System.Threading;
Using System.Net;
Using System.Xml;
Using Update;
Namespace Updatetest
{
public partial class Form1:form
{
Public Form1 ()
{
InitializeComponent ();
Checkupdate ();
}
public void Checkupdate ()
{
Softupdate app = new Softupdate (Application.executablepath, "Blogwriter");
App. Updatefinish + = new Updatestate (app_updatefinish);
Try
{
if (app. Isupdate && MessageBox.Show ("Check to new version, update?" "," Update ", Messageboxbuttons.yesno, messageboxicon.question) = = Dialogresult.yes)
{
Thread update = new Thread (app, ThreadStart. Update));
Update. Start ();
}
}
catch (Exception ex)
{
MessageBox.Show (ex. message);
}
}
void App_updatefinish () {
MessageBox.Show ("Update completed, please restart the program!") "," Update ", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}

Well, the whole program is over. Please leave me a message if you feel that there is something wrong or questionable.
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.