■ WebView2 미설치시 자동 설치하는 방법을 보여준다.
▶ MainApplication.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<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> |