前言
原来做的C#的WinForm程序就在考虑自动升级的事,然后为了省事在网上找了一个现成免费的自动升级程序(OAUS),前天突然间不能用了,非常尴尬,后来才一查,免费的只能用3-6个月,咨询了一下正式费的费用,有点太贵,还是一台服务端的价格,所以决定还是自己花点小时间做个在线升级得了.
思路
应用程序里面加入检测升级按钮,点击后就通过Process启动我们做的升级程序
通过杀掉前进程的方式关闭到原应用程序
升级程序获取到云服务器上的版本信息,与本地的版本信息进行对比.
如果有新版本就进行下载(后台把升级文件打到ZIP包).
下载完后解压替换原文件.
重新启动原应用程序,然后杀掉当前我们的升级程序的进程.
开发
第三方工具包
新建一个WinForm项目,起名SumUpdater,下图为整个项目的目录
在升级程序中我们需要检测版本信息对比,我在后台的TXT文件里面用的是JSON数据,下载后还要解压ZIP文件,所以我们需要引用第三方程序Newtonsoft.Json和DotNetZip.
在引用里鼠标左键选择管理NuGet程序包
搜索到Newtonsoft.Json和DotNetZip安装即可
主界面
把主窗体改名为MainForm.cs,然后在界面中加入两个控件,一个label,一个progressbar.
然后重写了主窗全的构造函数
public MainForm(string serverIP, int serverPort, string _callBackExeName, string title, int oldversioncode)
增加了服务器的IP地址,端口号,升级完后运行的程序名称,标题信息及当前版本号五个参数.
app.config
在本地的config文件里面加入几个项,用于设置服务器的IP地址,端口号,以及升级完成后调用的EXE程序,还有当前版本号
然后在Program.cs启动项里面加入读取这些信息的参数,然后传递到主窗体中
static void Main()
{
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
string serverIP = ConfigurationManager.AppSettings["ServerIP"];
int serverPort = int.Parse(ConfigurationManager.AppSettings["ServerPort"]);
string callBackExeName = ConfigurationManager.AppSettings["CallbackExeName"];
string title = ConfigurationManager.AppSettings["Title"];
int VersionCode = int.Parse(ConfigurationManager.AppSettings ["Version"]);
MainForm form = new MainForm(serverIP, serverPort, callBackExeName, title, VersionCode);
Application.Run(form);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
检测并下载更新 Updater.cs
与服务器的网络通讯我们用的是WebClient方式
这个类里主要的两个方法GetUpdaterInfo()和DownLoadUpGrade(string url)
///
/// 检测升级信息
///
///
///
///
public void GetUpdaterInfo()
{
info = new CUpdInfo();
_client = new WebClient();
//获取检测升级的字符串 _checkurl为地址
string json = _client.DownloadString(_checkurl);
//序列化json
info = SerializationHelper.Deserialize(json, 0);
//判断服务器上的版本号如果大于本地版本号,执行DownLoadUpGrade(),参数是info.appdownloadurl下载地址
if (info.versionCode > _oldversioncode)
{
DownLoadUpGrade(info.appdownloadurl);
}
else
{
_lbltext.Text = "当前为最新版本,无需升级!";
//等待500毫秒后直接启动原程序
Thread.Sleep(1500);
UpdaterOver.StartApp(_appFileName);
}
}
///
/// 下载升级文件
///
///
///
public void DownLoadUpGrade(string url)
{
_client = new WebClient();
if (_client.IsBusy)
{
_client.CancelAsync();
}
_client.DownloadProgressChanged +=
new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);
_client.DownloadFileCompleted += new AsyncCompletedEventHandler(webClient_DownloadFileCompleted);
//开始下载
_client.DownloadFileAsync(new Uri(url), _downfilename);
}
///
/// 下载进度条
///
///
///
private void webClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
_progressBar.Value = e.ProgressPercentage;
_lbltext.Text = $"正在下载文件,完成进度/";
}
///
/// 下载完成后的事件
///
///
///
private void webClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Cancelled)
{
_lbltext.Text = "下载被取消!";
}
else
{
_lbltext.Text = "下载完成!";
try
{
Thread.Sleep(1000);
UpdaterOver.StartOver(_downfilename, _appDirPath, info.versionCode, _appFileName);
}
catch (Exception ex)
{
_lbltext.Text = ex.Message;
}
}
}
下载完成 UpdaterOver.cs
///
///
///
///
///
public static void StartOver(string zipfile, string destpath, int versioncode, string appfile)
{
//解压文件到指定目录
ZipHelper.ZipHelper.UnZipFile(zipfile, destpath, true);
//成功后修改本地版本信息
UpdateVersion(versioncode);
//重新启动源程序
if (appfile != "")
{
StartApp(appfile);
}
}
下载完后的事件,首先解压ZIP替换文件
然后修改本地的版本号信息
最后再重新启动原程序
以下为源程序的源码:
里面包含了更新程序的源码和后台的txt里JSON字符串的格式,后台服务发布自行搜索一下
-END-
长按下方二维码关注
领取专属 10元无门槛券
私享最新 技术干货