IoTClientTool Auto Upgrade Update

What is IoTClientTool

IoTClientTool What's this, IoTClientTool yes IoTClient Implementation of visual operations for open source components.It is convenient to test and debug plc device, ModBusRtu, BACnet, serial port and other protocols.

Package into single file exe

Usually we develop WinForm programs, there are many dll files in addition to one exe file.
So is there a way to generate only one exe file to make the program easier to spread and use, the answer is yes.
NuGet searches Costura.Fody, downloads it, and regenerates the solution. You go to the bin directory to see that the original stack of DLLs is missing.

Official.net core support for packaging into single files

If you use.net core 3.0, you can use the officially supported publishing order file functionality directly.
Use the command dotnet publish-r win10-x64/p:PublishSingleFile=true directly
Or modify the project file

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
    <RuntimeIdentifier>win10-x64</RuntimeIdentifier>//Publishing Platform
    <PublishSingleFile>true</PublishSingleFile>//Is it a single exe
  </PropertyGroup>
  <PropertyGroup>
    <PublishTrimmed>true</PublishTrimmed>//Enable compression
  </PropertyGroup>
</Project>

Automatic Upgrade Update

A living desktop program should be upgraded automatically.Many people perform a single upgrade exe when making automatic upgrade updates, which means a complete program consists of at least two exes.Individuals don't feel elegant enough. Wouldn't it be perfect to update themselves with an exe?Think as follows:

Update yourself, then kill yourself and start a new self.
The code can be referenced in https://github.com/zhaopeiym/IoTClient/blob/master/IoTClient.Tool/IndexForm.cs CheckUpgradeAsync Method.

/// <summary>
///Check if upgrade is currently required
/// </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 There is a new version. Do you want to upgrade to the latest version?", "Version Upgrade", MessageBoxButtons.OKCancel) == DialogResult.OK)
        {
            if (new UpgradeForm().ShowDialog() != DialogResult.OK) return;
            var newApp = Application.StartupPath + @"\temp." + Path.GetFileName(Application.ExecutablePath);
            //Open Temporary File Close and Old Version
            Process.Start(newApp);
            Close();
            Environment.Exit(0);
        }
    }
}

/// <summary>
///Upgrade File Processing
/// </summary>
private void UpgradeFileManage()
{
    //If you start an upgrade temporary file,
    //Then 1, delete the old version 2, copy the current temporary file to the new version 3, start the new version 4, close the currently open temporary version
    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 Delete old versions
                if (File.Exists(filePath)) File.Delete(filePath);
            }
            catch (Exception)
            {
                //Hibernate and try again if the process is in use
                //The reason for this problem is that before the last program was closed, the program started. After starting, the program will execute deleting the previous program, so an error occurred.
                Thread.Sleep(500);
                if (File.Exists(filePath)) File.Delete(filePath);
            }
            //3. Copy temporary files to open new files for new ones       
            File.Copy(Application.ExecutablePath, newFilePath);
            //3. Open a new file
            Process.Start(filePath);
            //4. Close Temporary Files   
            //Close();
            Environment.Exit(0);
        }
        catch (Exception ex)
        {
            MessageBox.Show("Upgrade failed " + ex.Message);
        }
    }
    //4.2 Delete temporary files if they are not currently started.
    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);
        }
    }
} 

Design sketch

Tags: C# SDK github JSON Hibernate

Posted on Wed, 06 May 2020 22:16:10 -0400 by twister47