본문 바로가기
C#

[C#] Webview2 runtime 설치 여부 및 설치

by Jcoder 2021. 8. 31.


해당 코드는 .NET 6, .NET Framework로 바꿔서 사용해도 됨.
if (!RuntimeSetup.CheckInstallWebview2Runtime()) //로 체크 후
await RuntimeSetup.InstallWebview2RuntimeAsync(); 설치

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UpdateProgram
{
    public class RuntimeSetup
    {
        public static bool CheckInstallWebview2Runtime()
        {
            bool result = false;
            if (!System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                return result;
            string subKey;

            // 64비트 OS 인지 확인
            if (Environment.Is64BitOperatingSystem)
            {
                subKey = @"SOFTWARE\WOW6432Node\Microsoft\EdgeUpdate\Clients\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}"; // 64 bit
                using (var ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).OpenSubKey(subKey))
                {
                    if (ndpKey != null && ndpKey.GetValue("pv") != null)
                    {
                        StaticLogManager.WriteInfoLogToEventViewer($"64 bit version : {ndpKey.GetValue("pv")}");
                        result = true;
                    }
                    else
                    {
                        StaticLogManager.WriteWaringLogToEventViewer("64 bit not installed");
                    }
                }
            }
            else
            {
                subKey = @"SOFTWARE\Microsoft\EdgeUpdate\Clients\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}"; // 32 bit
                using (var ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subKey))
                {
                    if (ndpKey != null && ndpKey.GetValue("pv") != null)
                    {
                        StaticLogManager.WriteInfoLogToEventViewer($"32 bit version : {ndpKey.GetValue("pv")}");
                        result = true;
                    }
                    else
                    {
                        StaticLogManager.WriteWaringLogToEventViewer("32 bit not installed");
                    }
                }
            }

            return result;
        }

        public static async Task InstallWebview2RuntimeAsync()
        {
            if (!System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                return;
            string firstPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            string appName = Path.Combine(firstPath, "MicrosoftEdgeWebview2Setup.exe");

            try
            {
                using (HttpClient httpClient = new HttpClient())
                {
                    var result = await httpClient.GetAsync("https://go.microsoft.com/fwlink/p/?LinkId=2124703");
                    result.EnsureSuccessStatusCode();

                    using (var stream = await result.Content.ReadAsStreamAsync())
                    {
                        using (var fs = new FileStream(appName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
                        {
                            await stream.CopyToAsync(fs);
                        }
                    }

                    StaticLogManager.WriteInfoLogToEventViewer("Webview2Runtime 다운로드 완료");

                    if (File.Exists(appName))
                    {
                        StaticLogManager.WriteInfoLogToEventViewer("Webview2Runtime 설치 시작");
                        await Process.Start(appName, " /silent /install").WaitForExitAsync(); // net 6
                        // Process.Start(appName, " /silent /install").WaitForExit(); // net frameWork

                        StaticLogManager.WriteInfoLogToEventViewer("Webview2Runtime 설치 완료");
                        if (File.Exists(appName))
                        {
                            File.Delete(appName);
                            StaticLogManager.WriteInfoLogToEventViewer("MicrosoftEdgeWebview2Setup.exe 삭제 완료");
                        }
                    }
                }
            }
            catch (HttpRequestException hre)
            {
                StaticLogManager.WriteInfoLogToEventViewer($"{nameof(HttpRequestException)} - {hre.Message}");
            }
            catch (Exception e)
            {
                StaticLogManager.WriteInfoLogToEventViewer($"InstallWebview2RuntimeAsync Error : {e.Message}{Environment.NewLine} Data : {e.Data}{Environment.NewLine} StackTrace : {e.StackTrace}");
            }
        }
}


webview2 런타임 설치 방법
1. 온라인(해당 코드)
2. 오프라인
3. 지정버전 설치

Distribute a WebView2 app and the WebView2 Runtime - Microsoft Edge Development | Microsoft Docs

 

Distribute a WebView2 app and the WebView2 Runtime - Microsoft Edge Development

Distribution options when releasing an app using Microsoft Edge WebView2

docs.microsoft.com