첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

■ WebView2 미설치시 자동 설치하는 방법을 보여준다.

TestProject.zip
0.00MB

▶ MainApplication.xaml.cs

using System;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows;

using Microsoft.Win32;

namespace TestProject
{
    /// <summary>
    /// 메인 애플리케이션
    /// </summary>
    public partial class MainApplication : Application
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Protected

        #region 시작시 처리하기 - OnStartup(e)

        /// <summary>
        /// 시작시 처리하기
        /// </summary>
        /// <param name="e">이벤트 인자</param>
        protected async override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            if(!CheckWebview2Runtime())
            {
                await InstallWebview2RuntimeAsync();
            }
        }

        #endregion

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 웹뷰2 런타임 설치 여부 구하기 - CheckWebview2Runtime()

        /// <summary>
        /// 웹뷰2 런타임 설치 여부 구하기
        /// </summary>
        /// <returns>웹뷰2 런타임 설치 여부</returns>
        private bool CheckWebview2Runtime()
        {
            if(Environment.Is64BitOperatingSystem)
            {
                string subsidaryKey = @"SOFTWARE\WOW6432Node\Microsoft\EdgeUpdate\Clients\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}";

                using(RegistryKey registryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).OpenSubKey(subsidaryKey))
                {
                    return (registryKey != null && registryKey.GetValue("pv") != null);
                }
            }
            else
            {
                string subsidaryKey = @"SOFTWARE\Microsoft\EdgeUpdate\Clients\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}";

                using(RegistryKey registryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subsidaryKey))
                {
                    return (registryKey != null && registryKey.GetValue("pv") != null);
                }
            }
        }

        #endregion
        #region 웹뷰2 런타임 설치하기(비동기) - InstallWebview2RuntimeAsync()

        /// <summary>
        /// 웹뷰2 런타임 설치하기(비동기)
        /// </summary>
        /// <returns>태스크</returns>
        private async Task InstallWebview2RuntimeAsync()
        {
            string executabnlePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string setupFilePath   = Path.Combine(executabnlePath ?? string.Empty, "MicrosoftEdgeWebview2Setup.exe");

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

                    responseMessage.EnsureSuccessStatusCode();

                    using(Stream stream = await responseMessage.Content.ReadAsStreamAsync())
                    {
                        using(FileStream fileStream = new FileStream(setupFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
                        {
                            await stream.CopyToAsync(fileStream);
                        }
                    }

                    if(File.Exists(setupFilePath))
                    {
                        await Process.Start(setupFilePath, " /silent /install").WaitForExitAsync();
                    }
                }
            }
            catch(Exception exception)
            {
                MessageBox.Show(exception.ToString());
            }
        }

        #endregion
    }
}

 

▶ MainWindow.xaml

<Window x:Class="TestProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:control="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
    Width="800"
    Height="600"
    Title="TestProject"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <control:WebView2 Source="https://icodebroker.tistory.com" />
</Window>
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요