IoTClientTool自动升级更新

时间:2023-11-26 12:15:32

IoTClientTool是什么

IoTClientTool是什么,IoTClientToolIoTClient开源组件的可视化操的作实现。方便对plc设备和ModBusRtu、BACnet、串口等协议进行测试和调试。

打包成单文件exe

通常我们开发出来的WinForm程序,除了一个exe文件还会有很多dll文件。

那么有没有办法只生成一个exe文件,让程序更加方便传播和使用,答案是肯定的。

NuGet搜索Costura.Fody并下载,然后重新生成解决方案即可,你在去bin目录查看,原来的一堆dll不见了。

IoTClientTool自动升级更新

.net core官方支持打包成单文件

如果你使用的.net core 3.0,那么你可以直接使用官方支持的发布单文件功能。

直接使用命令dotnet publish -r win10-x64 /p:PublishSingleFile=true

或者修改一下项目文件

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<RuntimeIdentifier>win10-x64</RuntimeIdentifier>//发布平台
<PublishSingleFile>true</PublishSingleFile>//是否单个exe
</PropertyGroup>
<PropertyGroup>
<PublishTrimmed>true</PublishTrimmed>//启用压缩
</PropertyGroup>
</Project>

自动升级更新

一个有生命的桌面程序理应做到可以自动升级。很多人在做自动升级更新时会执行一个单独的升级exe,也就是说一个完整的程序起码包括两个exe。个人觉得不够优雅,如果能用一个exe自己更新自己岂不是完美。思考如下:

IoTClientTool自动升级更新

自己更新自己 ,然后杀了自己,启动新的自己。

代码可参考https://github.com/zhaopeiym/IoTClient/blob/master/IoTClient.Tool/IndexForm.cs中的CheckUpgradeAsync方法。

/// <summary>
/// 检查当前是否需要升级
/// </summary>
private async Task CheckUpgradeAsync()
{
UpgradeFileManage();
HttpClient http = new HttpClient();
var content = new StringContent(JsonConvert.SerializeObject(new VersionCheckInput()));
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var response = await http.PostAsync("https://download.haojima.net/api/IoTClient/VersionCheck", content);
var result = await response.Content.ReadAsStringAsync();
var VersionObj = JsonConvert.DeserializeObject<ResultBase<VersionCheckOutput>>(result);
if (VersionObj.Code == 200 && VersionObj.Data.Code == 1)
{
if (MessageBox.Show("IoTClient有新版本,是否升级到最新版本?", "版本升级", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
if (new UpgradeForm().ShowDialog() != DialogResult.OK) return;
var newApp = Application.StartupPath + @"\temp." + Path.GetFileName(Application.ExecutablePath);
//打开临时文件 关闭并旧版本
Process.Start(newApp);
Close();
Environment.Exit(0);
}
}
} /// <summary>
/// 升级文件处理
/// </summary>
private void UpgradeFileManage()
{
//如果启动的升级临时文件,
//则1、删除旧版本 2、复制当前临时文件为新版本 3、启动新版本 4、关闭当前打开的临时版本
if (Path.GetFileName(Application.ExecutablePath).Contains("temp."))
{
var filePath = Path.Combine(Application.StartupPath, Path.GetFileName(Application.ExecutablePath).Replace("temp.", ""));
var newFilePath = filePath;
try
{
try
{
//2.1删除旧版本
if (File.Exists(filePath)) File.Delete(filePath);
}
catch (Exception)
{
//如果因为进程正在使用中则休眠后再重试
//出现此问题的原因是,上一个程序还没关闭,这个程序就启动了,启动后会执行删除上一个程序,所以报错。
Thread.Sleep(500);
if (File.Exists(filePath)) File.Delete(filePath);
}
//3、复制临时文件为新的文件 打开新的文件
File.Copy(Application.ExecutablePath, newFilePath);
//3、打开新的文件
Process.Start(filePath);
//4、关闭临时文件
//Close();
Environment.Exit(0);
}
catch (Exception ex)
{
MessageBox.Show("升级失败 " + ex.Message);
}
}
//4.2如果当前启动的不是临时文件,则删除临时文件。
else
{
var filePath = Path.Combine(Application.StartupPath, "temp." + Path.GetFileName(Application.ExecutablePath));
try
{
if (File.Exists(filePath)) File.Delete(filePath);
}
catch (Exception)
{
Thread.Sleep(500);
if (File.Exists(filePath)) File.Delete(filePath);
}
}
}

效果图

IoTClientTool自动升级更新