自己也是个小白,程序简陋,不喜勿喷!
需要两个程序: 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