page contents

c# winform 升级

本文讲述了c# winform 升级!具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:

attachments-2022-10-NeZ4ecV263477146d332c.png本文讲述了c# winform 升级!具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:

1. 流程描述

attachments-2022-10-JnCVdY0l634770f4a4ad4.png

2.升级配置结构

{
  "err_code": 200,
  "err_msg": "success",
  "version": "2.1.2",
  "download_url": "http://******/upgrade_package/Upgrade2.1.2.zip",
  "fingerprint": "d5af2da283ed4da97dbb1e445620669f"
}

3.参考资料

https://download.csdn.net/download/qq592691117/10623907

4.参考程序修正

主要问题:没有使用线程,从而升级过程界面会假死。
使用线程方式,将过程放入 Upgrade() 方法中,在线程中调用,有点小优雅吧。

private void StartUpgrade()
{
    Thread thread = new Thread(new ThreadStart(() => {
        try
        {
            Upgrade();
            // writeLog("更新成功!");
            ShowTip("更新成功!", Colors.Green);
        }
        catch (Exception ex)
        {
            common.log.LogTextHelper.WriteLine(ex.Message);
            ShowTip(ex.Message, Colors.Red);
        }
        finally
        {
            DeleteFile();
        }

        Thread.Sleep(3000);
        this.Dispatcher.Invoke(new Action(() =>
        {
            this.Close();
        }));
    }));

    thread.Start();
}

进度条UI的更新(调用此方法在线程中更新)

private void AddPercent()
{
    this.Dispatcher.Invoke(new Action(() =>
    {
        pgbUpdate.Value++;
    }));
}

不允许同时打开多次(在App.xaml.cs中)

public partial class App : Application
{
    System.Threading.Mutex mutex;
    //重写OnStartup,获得启动程序  
    protected override void OnStartup(StartupEventArgs e)
    {
        bool ret;
        mutex = new System.Threading.Mutex(true, "AutoUpdate", out ret);
        if (!ret) // 不可多次打开
        { 
            Environment.Exit(0);
        }
        else
        {
            if (e.Args != null && e.Args.Count() > 0)
            {
                var lastVersion = e.Args[0];  // 取得传递的版本号
            }
            base.OnStartup(e);
        }
    }
}

5.其它

指纹验证

取得文件的 MD5 值

public class FileHelper
{
    static public string GetMD5WithFilePath(string filePath)
    {
        using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] hash_byte = md5.ComputeHash(file);
            string str = System.BitConverter.ToString(hash_byte);
            str = str.Replace("-", "").ToLower();
            return str;
        }
    }
}

在线程中更新文本显示(支持指定颜色,如 Colors.Red)

private void ShowTip(string tip, Color color)
{
    this.Dispatcher.Invoke(new Action(() =>
    {
        lbl_name.Foreground = new SolidColorBrush(color);
        lbl_name.Content = tip;
    }));
}

更多相关技术内容咨询欢迎前往并持续关注六星社区了解详情。

长按或扫描下方二维码,免费获取 Python公开课和大佬打包整理的几百G的学习资料,内容包含但不限于Python电子书、教程、项目接单、源码等等

attachments-2022-10-kwwbZ9WG6347756cbf77c.jpg


  • 发表于 2022-10-13 10:00
  • 阅读 ( 295 )
  • 分类:C/C++开发

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
轩辕小不懂
轩辕小不懂

2403 篇文章

作家榜 »

  1. 轩辕小不懂 2403 文章
  2. Pack 1131 文章
  3. 小柒 1046 文章
  4. Nen 576 文章
  5. 王昭君 209 文章
  6. 文双 71 文章
  7. 小威 64 文章
  8. Cara 36 文章