asp.net of c program version upgrade update implementation code

  • 2020-05-09 18:29:17
  • OfStack

Direct access code:
 
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 the event triggered  
    /// </summary> 
    public delegate void UpdateState(); 
    /// <summary> 
    ///  Application update  
    /// </summary> 
    public class SoftUpdate 
    { 
        private string download; 
private const string updateUrl = "//www.ofstack.com/update.xml";// Upgrade configured XML Address of the file  
        #region  The constructor  
        public SoftUpdate() { } 
        /// <summary> 
        ///  Application update  
        /// </summary> 
        /// <param name="file"> The file to update </param> 
        public SoftUpdate(string file,string softName) { 
            this.LoadFile = file; 
this.SoftName = softName; 
        } 
        #endregion 
        #region  attribute  
        private string loadFile; 
        private string newVerson; 
private string softName; 
        private bool isUpdate; 
        /// <summary> 
        ///  Or whether it needs to be updated  
        /// </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 version of assembly  
        /// </summary> 
        public string NewVerson 
        { 
            get { return newVerson; } 
        } 
/// <summary> 
///  Name of upgrade  
/// </summary> 
public string SoftName 
{ 
get { return softName; } 
set { softName = value; } 
} 
        #endregion 
        /// <summary> 
        ///  The event that fires when the update is complete  
        /// </summary> 
        public event UpdateState UpdateFinish; 
        private void isFinish() { 
            if(UpdateFinish != null) 
                UpdateFinish(); 
        } 
        /// <summary> 
        ///  Download the update  
        /// </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 to see if an update is needed  
        /// </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(" There is an error in the update, please make sure the network connection is correct and try again! "); 
            } 
        } 
        /// <summary> 
        ///  Gets the file to update  
        /// </summary> 
        /// <returns></returns> 
        public override string ToString() 
        { 
            return this.loadFile; 
        } 
    } 
} 

Compile the code into a class library file, which is referred to by the program OK.
The parameter passed in is already commented.
The following is the updated XML file file capacity, to the space above can be, get the XML file address.
 
<?xml version="1.0" encoding="utf-8" ?> 
<Update> 
<Soft Name="BlogWriter"> 
<Verson>1.0.1.2</Verson> 
<DownLoad>//www.ofstack.com/BlogWrite.rar</DownLoad> 
</Soft> 
</Update> 

Program update call method:
1. Start with DLL above.
2. The code of the calling method 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(" New version has been checked. Is it 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 completed, please restart the program! ", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information); 
        } 
    } 
} 

Okay, that's the end of the process. If you feel anything is wrong or have any questions, please leave me a message.

Related articles: