IoTClientTool自動升級更新


IoTClientTool是什么

IoTClientTool是什么,IoTClientToolIoTClient開源組件的可視化操的作實現。方便對plc設備和ModBusRtu、BACnet、串口等協議進行測試和調試。

打包成單文件exe

通常我們開發出來的WinForm程序,除了一個exe文件還會有很多dll文件。
那么有沒有辦法只生成一個exe文件,讓程序更加方便傳播和使用,答案是肯定的。
NuGet搜索Costura.Fody並下載,然后重新生成解決方案即可,你在去bin目錄查看,原來的一堆dll不見了。

.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自己更新自己豈不是完美。思考如下:

自己更新自己 ,然后殺了自己,啟動新的自己。
代碼可參考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);
        }
    }
} 

效果圖


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM