C ා implementation of Winform program automatic version upgrade and update

C ා implementation of Winform program automatic version upgrade and update

1, First, create a new Wnform main program TestMainProgarm. The interface is as follows:

To view the program version from the AssemblyInfo.cs file:

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

string ver = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(); version number can be obtained;
Before running the main program, we can check whether the version of the running program is consistent with that of the server or the version is relatively low. The main program code is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using Update;

namespace TestMainProgarm
{
    static class Program
    {
        /// <summary>
        ///The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            if (checkUpdateLoad())
            {
                Application.Exit();
                return;
            }
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new FrmMain());
        }

        public static bool checkUpdateLoad()
        {
            bool result = false;
            SoftUpdate app = new SoftUpdate(Application.ExecutablePath, "TestMainProgarm");
            try
            {
                if (app.IsUpdate && MessageBox.Show("Check for new version, update?", "Version checking", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    string path = Application.StartupPath.Replace("program", "");
                    System.Diagnostics.Process process = new System.Diagnostics.Process();
                    process.StartInfo.FileName = "autoUpdate.exe";
                    process.StartInfo.WorkingDirectory = path;//To remove the used exe path, for example: "C:\windows";               
                    process.StartInfo.CreateNoWindow = true;
                    process.Start();

                    result = true;
                }
                else
                {
                    result = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                result = false;
            }
            return result;
        }
    }
}

2, Automatically upgraded programs:

The code is as follows:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Update;

namespace autoUpdate
{
    public partial class Form1 : Form
    {
        [DllImport("zipfile.dll")]
        public static extern int MyZip_ExtractFileAll(string zipfile, string pathname);
        public Form1()
        {
            InitializeComponent();
            //Clear the previously downloaded rar file
            if (File.Exists(Application.StartupPath + "\\Update_autoUpdate.zip"))
            {
                try
                {
                    File.Delete(Application.StartupPath + "\\Update_autoUpdate.zip");
                }
                catch (Exception)
                {


                }

            }
            if (Directory.Exists(Application.StartupPath + "\\autoupload"))
            {
                try
                {
                    Directory.Delete(Application.StartupPath + "\\autoupload", true);
                }
                catch (Exception)
                {


                }
            }

            //Check whether the server has a new version of the program
            checkUpdate();
            timer1.Enabled = true;
        }
        SoftUpdate app = new SoftUpdate(Application.ExecutablePath, "TestMainProgarm");
        public void checkUpdate()
        {

            app.UpdateFinish += new UpdateState(app_UpdateFinish);
            try
            {
                if (app.IsUpdate)
                {
                    app.Update();
                }
                else
                {
                    MessageBox.Show("No new version detected!");
                    Application.Exit();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void app_UpdateFinish()
        {
            //Unzip the downloaded file
            string path = app.FinalZipName;
            if (File.Exists(path))
            {
                //After that, decompress the filter, embed zip into ini, and then recompress
                string dirEcgPath = Application.StartupPath + "\\" + "autoupload";
                if (!Directory.Exists(dirEcgPath))
                {
                    Directory.CreateDirectory(dirEcgPath);
                }
                //Start unpacking
                MyZip_ExtractFileAll(path, dirEcgPath);

                try
                {
                    //Copy new file replace old file
                    DirectoryInfo TheFolder = new DirectoryInfo(dirEcgPath);
                    foreach (FileInfo NextFile in TheFolder.GetFiles())
                    {
                        File.Copy(NextFile.FullName, Application.StartupPath + "\\program\\" + NextFile.Name, true);
                    }
                    Directory.Delete(dirEcgPath, true);
                    File.Delete(path);
                    //Overwrite complete restart program
                    path = Application.StartupPath + "\\program";
                    System.Diagnostics.Process process = new System.Diagnostics.Process();
                    process.StartInfo.FileName = "TestMainProgarm.exe";
                    process.StartInfo.WorkingDirectory = path;//To remove the used exe path, for example: "C:\windows";               
                    process.StartInfo.CreateNoWindow = true;
                    process.Start();

                    Application.Exit();
                }
                catch (Exception)
                {
                    MessageBox.Show("Please shut down the system and perform update operation!");
                    Application.Exit();
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            label2.Text = "Download File progress:" + COMMON.CommonMethod.autostep.ToString() + "%";
            this.progressBar1.Value = COMMON.CommonMethod.autostep;
            if (COMMON.CommonMethod.autostep == 100)
            {
                timer1.Enabled = false;
            }
        }
    }
}

3, First, place an XML file on the server to store the software version number and the latest program path. At present, the program on the website is version 1.0.0.3. I built a new website on the local IIS for testing. The website path is: http://192.168.1.63:8050/update.xml. The content of the XML file is as follows:

<?xml version="1.0" encoding="utf-8" ?>
<Update>
  <Soft Name="TestMainProgarm">
    <Verson>1.0.0.3</Verson>
    <DownLoad>http://192.168.1.63:8050/Update_autoUpdate.zip</DownLoad>
  </Soft>
</Update>

The documents in the website are as follows:

48 original articles published, 5 praised, 10000 visitors+
Private letter follow

Tags: Windows xml IIS encoding

Posted on Mon, 20 Jan 2020 13:20:21 -0500 by wwfc_barmy_army