C# 桌面程序自动更新

本文详细描述了一个使用VisualStudio2022开发的.NETFramework4.7.2应用程序的更新流程,包括客户端a.exe和c.exe的交互,以及服务器端的1.xml文件在版本检测中的作用。开发者分享代码以帮助新手理解和学习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自己也是个小白,程序简陋,不喜勿喷!

需要两个程序:  a.exe  运行程序;   c.exe 更新程序;   1.xml

开发Visual Studio 2022,框架 .NET Framework 4.7.2

一、实现原理:
1、代码端:
a.exe  运行检测版本号: 

服务器版本  ==  当前版本:提示最新版本,删除c.exe 更新程序;

服务器版本  !=  当前版本:提示有更新版本,下载c.exe 更新程序,打开c.exe,关闭当前程序;

c.exe  运行,提示更新信息,确定后更新操作

删除原来程序(上一个版本程序 a.exe),下载新版本程序a.exe(老版本以删除,可以直接下载);

2、服务器端:

1.xml  提示更新最新版本号

需要上传:1.xml版本文件,c.exe更新文件,a.exe最新版本文件;

//以上为更新原理;

二、代码实现:
1、a.exe
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Collections;
using System.IO;
using System.Xml;
using System.Diagnostics;
using System.Threading;
using System.Xml.Linq;
using static System.Net.Mime.MediaTypeNames;
using System.Threading.Tasks;


namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        //使用WebClient下载
        WebClient client = new WebClient();
        ArrayList downlist = new ArrayList();
        private const string CurrentVersion = "4.0.0.0";//版本号,测试此处写老版本,服务器上需要生成新版本


        public Form1()
        {
            InitializeComponent();

            CheckLatestVersion(); //检查最新版本
            label1.Text = CurrentVersion; // 展示当前版本
           

        }



        /// <summary>
        /// 读取从服务器获取的最新版本号
        /// </summary>
       
        public void CheckLatestVersion()
        {
            try
            {
                client.DownloadFile("http://api.yuming.com/1.xml", "2.xml"); //服务器上下载1.xml,重命名为2.xml
                OnDownloadCompleted();
            }
            catch (Exception ex)
            {
                MessageBox.Show("下载文件时发生错误: " + ex.Message, "错误");
                this.Invoke(new Action(() => this.Close())); // 确保关闭操作在 UI 线程上执行
            }
        }


        private void OnDownloadCompleted()
        {
            string latestVersion = string.Empty;
            try
            {
                if (File.Exists("2.xml"))
                {
                        XmlDocument doc = new XmlDocument();
                        //加载要读取的XML
                        doc.Load("2.xml");

                        //获得根节点
                        XmlElement version = doc.DocumentElement;

                        //获得子节点 返回节点的集合
                        XmlNodeList Update = version.ChildNodes;

                        foreach (XmlNode item in Update)
                        {
                            latestVersion = item.InnerText;
                        }
                }
                // 显示最新版本号
                if (label2 != null) // 确保label2在当前上下文中是可访问的
                {
                    label2.Text = latestVersion;
                }
                // 检查是否需要更新
                if (latestVersion != CurrentVersion)
                {
                    // 显示有更新信息
                    if (label7 != null) // 确保label7在当前上下文中是可访问的
                    {
                        label7.Text = "有新版本。";

                    }
                    // 这里可以调用下载和安装的方法
                    // DownloadInstall();
                }
                else
                {
                    // 显示无更新信息
                    if (label7 != null) // 确保label7在当前上下文中是可访问的
                    {
                        label7.Text = "您已经是最新版本。";
                    }
                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("解析文件时发生错误: " + ex.Message, "错误");
            }
            finally
            {
                // 删除临时文件前检查是否存在
                if (File.Exists("2.xml")) File.Delete("2.xml");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (label2.Text == label1.Text)
            {
                File.Delete("c.exe");
                MessageBox.Show("最新版本 " );
            }
            else
            {
                //检查最新版本
                client.DownloadFile("http://api.yuming.com/c.exe", "c.exe");//下载更新程序,打开更新程序,关闭当前程序
                Process installProcess = new Process();
                installProcess.StartInfo.FileName = "c.exe";
                installProcess.StartInfo.UseShellExecute = false; // 如果需要的话,设置为false以允许重定向输入/输出/错误流
                installProcess.Start();
                this.Close();
            }
            }
        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

 下图为程序控件,布局,没有修改命名,看的清楚

打开程序

更新后

以上为a.exe全部代码,没有保留。

 2、c.exe
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection.Emit;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace WindowsFormsApp3
{
    public partial class Form1 : Form

    {
        //使用WebClient下载
        WebClient client = new WebClient();
        ArrayList downlist = new ArrayList();



        public Form1()
        {
            InitializeComponent();

            
    }

        private void button1_Click(object sender, EventArgs e)
        {
            shanchu();
        }


        public void shanchu()
        {

            //progressBar1.Value = 20;
            //label1.Text = "20 %";
            //Thread.Sleep(1000); // 参数是以毫秒为单位的,所以1000毫秒等于1秒
            File.Delete("1.exe");
            client.DownloadFile("http://api.yuming.com/1.exe", "1.exe");//下载更新程序,打开更新程序,关闭当前程序
            Process installProcess = new Process();
            installProcess.StartInfo.FileName = "1.exe";
            installProcess.StartInfo.UseShellExecute = false; // 如果需要的话,设置为false以允许重定向输入/输出/错误流
            installProcess.Start();
            MessageBox.Show("更新完成! ");
            this.Close();
        }



    }
}

c.exe控件,布局 

运行后

运行完成

三、服务器端实现

1.xml

<?xml version="1.0" encoding="utf-8" ?> 
<Root>
<Version>5.0.0.0</Version>
</Root>

以上为全部代码,没有保留直接粘贴,上传就可以使用。

分享出来原因有几个

1、网上找了很多,都要会员,下载等等,对于新手小白,就是劝退开发的热情(充了会员发现下载还有给钱,人生到处都是坑,没有真诚);

2、一个功能弄了三天,始终过不了错误的砍(思路很重要,偏了,怎么都对不了,总想着一个窗口搞定,是不可能的);

3、希望对新手有帮助,工程文件也会共享下载。富裕的支持下,不富裕的自己复制也是可以用。


点击富裕通道https://download.youkuaiyun.com/download/q234579464573499/89070273

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值