Implementation Code of asp.net (c #) program version upgrade and update

Source: Internet
Author: User

Directly run the Code:
Copy codeThe Code is 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>
/// Events triggered by update completion
/// </Summary>
Public delegate void UpdateState ();
/// <Summary>
/// Program update
/// </Summary>
Public class SoftUpdate
{
Private string download;
Private const string updateUrl = "http://www.jb51.net/update.xml"; // update the XML file address of the Configuration
# Region Constructor
Public SoftUpdate (){}
/// <Summary>
/// Program update
/// </Summary>
/// <Param name = "file"> file to be updated </param>
Public SoftUpdate (string file, string softName ){
This. LoadFile = file;
This. SoftName = softName;
}
# Endregion
# Region attributes
Private string loadFile;
Private string newVerson;
Private string softName;
Private bool isUpdate;
/// <Summary>
/// Or whether an update is required
/// </Summary>
Public bool IsUpdate
{
Get
{
CheckUpdate ();
Return isUpdate;
}
}
/// <Summary>
/// Check the updated file
/// </Summary>
Public string LoadFile
{
Get {return loadFile ;}
Set {loadFile = value ;}
}
/// <Summary>
/// New assembly version
/// </Summary>
Public string NewVerson
{
Get {return newVerson ;}
}
/// <Summary>
/// Upgrade name
/// </Summary>
Public string SoftName
{
Get {return softName ;}
Set {softName = value ;}
}
# Endregion
/// <Summary>
/// Events triggered when the update is complete
/// </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 ("network connection failed due to an error during update! ");
}
}
/// <Summary>
/// Check whether 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 ("an error occurred during update. Please confirm that the network connection is correct and try again! ");
}
}
/// <Summary>
/// Obtain the file to be updated
/// </Summary>
/// <Returns> </returns>
Public override string ToString ()
{
Return this. loadFile;
}
}
}

Compile the code into a class library file and use the program to reference it.
The input parameter has been commented out.
Below is the updated XML file class capacity, which can be uploaded to the space to obtain the address of the XML file.
Copy codeThe Code is 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. First reference the above DLL.
2. The call method code is as follows:
Copy codeThe Code is 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 whether the new version is updated? "," Update ", MessageBoxButtons. YesNo, MessageBoxIcon. Question) = DialogResult. Yes)
{
Thread update = new Thread (new ThreadStart (app. Update ));
Update. Start ();
}
}
Catch (Exception ex)
{
MessageBox. Show (ex. Message );
}
}
Void app_UpdateFinish (){
MessageBox. Show ("update complete. Please restart the program! "," Update ", MessageBoxButtons. OK, MessageBoxIcon. Information );
}
}
}

Well, the whole program ends here. Leave a message if you have any questions.

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.