Microsoft Edge WebView2 Runtime is a Web control available for WinForm and WPF, allowing you to embed the Web in native applications. If you are familiar with c#, you can completely replace electron. Winform + webview2 is a good solution.

However, Webview2 is not installed with the operating system by default, nor is it automatically available once the Edge browser is installed. You must manually deploy the WebView2 Runtime, and there are two main deployment methods

  1. Download online and install
  2. Will MicrosoftEdgeWebview2Setup. The exe packaging to program, can install offline

Either way, you need to check whether the WebView2 Runtime is installed, and you can check the registry key value

A 64 - bit HKEY_LOCAL_MACHINE \ SOFTWARE \ WOW6432Node \ Microsoft \ EdgeUpdate \ Clients \ {F3017226 - FE2A - 4295-8 BDF - 00 c3a9a7e4c5} HKEY_CURRENT_USER \ Software \ Microsoft \ EdgeUpdate \ Clients \ {F3017226 - FE2A - 4295-8 BDF - 00 c3a9a7e4c5} 32 bits HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\EdgeUpdate\Clients\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5} HKEY_CURRENT_USER\Software\Microsoft\EdgeUpdate\Clients\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}Copy the code

Or directly use CoreWebView2Environment. GetAvailableBrowserVersionString (); Obviously the latter is simpler.

Automatic detection and installation of Edge Webview2 implementation ideas

  1. In the main form constructor, useCoreWebView2Environment.GetAvailableBrowserVersionString();If the value is null or null, it is not installed
        public static bool IsInstallWebview2()
        {

            string? res = "";
            try
            {
                res = CoreWebView2Environment.GetAvailableBrowserVersionString();
            }
            catch (System.Exception)
            {
            }
            if (res == "" || res == null)
            {
                return false;
            }
            return true;
        }
Copy the code
  1. Start an asynchronous task, via the url go.microsoft.com/fwlink/p/?L… Download and Install
  2. Pass after installationApplication.Restart();Restart the application
/ / Restart the Application. The Restart (); / / stop the current Process Process. GetCurrentProcess ()? .Kill();Copy the code
  1. Define one in the main formpublic static Form1 guiUsed to store the main form, in the download process, can dynamically update the main form title, to achieve the prompt
Form1.gui.Text = "Downloading and installing required components "; Form1.gui.label1.Visible = true; Form1.gui.label1.text = "Downloading required components "; form1.gui.label1.text =" downloading required components ";Copy the code

The main code

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 ="Downloading and installing required components";
                Form1.gui.label1.Visible = true;
                Form1.gui.label1.Text = "Downloading and installing required components";

                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())
                    {
                        / / restartApplication.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); }}}}Copy the code

reference

  1. Microsoft official WebView2 Runtime deployment docs.microsoft.com/zh-cn/micro…

  2. Install webView2 plugin in vs docs.microsoft.com/zh-cn/micro…