欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

自动检测和安装WebView2运行时

最编程 2024-08-02 10:22:20
...

Microsoft Edge WebView2 runtime 是可用于 winformWPF 的web控件,允许在本机应用中嵌入 web。如果熟悉 c#,完全可以代替electron。 winform + webview2 是很不错的方案。

不过 webview2 默认并未随操作系统安装,也非只要安装了 Edge浏览器后就能自动可用,必须要手动部署 webview2 runtime,主要有两种部署方式

  1. 联机下载后安装
  2. 将 MicrosoftEdgeWebview2Setup.exe 打包到程序中,可脱机状态下安装

无论哪种方式,均需要检测是否已安装了 webview2 runtime,可采用检测注册表项值

64位
HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\EdgeUpdate\Clients\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}
HKEY_CURRENT_USER\Software\Microsoft\EdgeUpdate\Clients\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}

32位
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\EdgeUpdate\Clients\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}
HKEY_CURRENT_USER\Software\Microsoft\EdgeUpdate\Clients\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}

或者直接使用 CoreWebView2Environment.GetAvailableBrowserVersionString(); 方法判断 显然后者更简单。

自动检测并安装 Edge Webview2 实现思路

  1. 在主窗体构造函数中,使用 CoreWebView2Environment.GetAvailableBrowserVersionString(); 判断为空或null则未安装
        public static bool IsInstallWebview2()
        {

            string? res = "";
            try
            {
                res = CoreWebView2Environment.GetAvailableBrowserVersionString();
            }
            catch (System.Exception)
            {
            }
            if (res == "" || res == null)
            {
                return false;
            }
            return true;
        }
  1. 启动一个异步任务,通过url go.microsoft.com/fwlink/p/?L… 下载并安装
  2. 安装完毕后通过 Application.Restart(); 重启应用
//重启
Application.Restart();
//停止当前进程
Process.GetCurrentProcess()?.Kill();
  1. 在 主窗体中定义一个 public static Form1 gui 用于存放主窗体,在下载过程中,可动态更新主窗体标题,实现提示
Form1.gui.Text = "正在下载安装必须组件";
Form1.gui.label1.Visible = true;
Form1.gui.label1.Text = "正在下载安装必须组件";

image.png

image.png

主要代码

using Microsoft.Web.WebView2.Core;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        public static Form1 gui;
        public Form1()
        {
            InitializeComponent();
            gui = this;
            this.label1.Visible = false;
            InstallCheck.InstallWebview2Async();
        }
    }

    public static class InstallCheck
    {
        public static bool IsInstallWebview2()
        {
            string? res = "";
            try
            {
                res = CoreWebView2Environment.GetAvailableBrowserVersionString();
            }
            catch (System.Exception)
            {
            }
            if (res == "" || res == null)
            {
                return false;
            }
            return true;
        }

        public static async Task InstallWebview2Async()
        {
            if (!IsInstallWebview2())
            {
                Form1.gui.Text = "正在下载安装必须组件";
                Form1.gui.label1.Visible = true;
                Form1.gui.label1.Text = "正在下载安装必须组件";

                using var webClient = new WebClient();
                bool isDownload = false;
                string MicrosoftEdgeWebview2Setup = System.IO.Path.Combine(Application.StartupPath, "MicrosoftEdgeWebview2Setup.exe");
                webClient.DownloadFileCompleted += (s, e) => { isDownload = true; };
                await webClient.DownloadFileTaskAsync("https://go.microsoft.com/fwlink/p/?LinkId=2124703", "MicrosoftEdgeWebview2Setup.exe"); 

                if (isDownload)
                {
                    Process.Start(MicrosoftEdgeWebview2Setup, " /silent /install").WaitForExit();  

                    if (IsInstallWebview2())
                    {
                        //重启
                        Application.Restart();
                        Process.GetCurrentProcess()?.Kill();
                        if (System.IO.File.Exists("MicrosoftEdgeWebview2Setup.exe")){
                            System.IO.File.Delete("MicrosoftEdgeWebview2Setup.exe");
                        }
                    }
                }
            }
        }

        public static void DeleteWebView2Folder()
        {
            string webview2Dir = $"{System.Environment.GetCommandLineArgs()[0]}.WebView2";
            if (Directory.Exists(webview2Dir))
            {
                Directory.Delete(webview2Dir, true);
            }
        }
    }

}

参考

  1. 微软官方webview2 runtime 部署 docs.microsoft.com/zh-cn/micro…

  2. vs中安装 webview2插件 docs.microsoft.com/zh-cn/micro…