728x90
반응형
728x170
■ WebView2 미설치시 자동 설치하는 방법을 보여준다.
▶ 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
반응형
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF/.NET6] ToolTipService 엘리먼트 : ShowDuration 첨부 속성을 사용해 툴팁 표시 시간 설정하기 (0) | 2022.10.03 |
---|---|
[C#/WPF/.NET6] ColorConverter 클래스 : ConvertFromString 정적 메소드를 사용해 16진수 색상 코드에서 색상 구하기 (0) | 2022.10.02 |
[C#/WPF/.NET6] Screen 클래스 : FromHandle 정적 메소드를 사용해 Window 객체의 화면 구하기 (0) | 2022.10.02 |
[C#/WPF/.NET6] WPF 프로젝트에서 WinForm 사용하기 (0) | 2022.10.02 |
[C#/WPF/.NET6] 누겟 설치 : Microsoft.Web.WebView2 (0) | 2022.09.30 |
[C#/WPF] Canvas 클래스 : 캔버스 확장 사용하기 (0) | 2022.08.25 |
[C#/WPF/.NET6] BitmapImage 클래스 : WINFORM Bitmap 객체에서 비트맵 이미지 구하기 (0) | 2022.08.20 |
[C#/WPF/.NET6] Image 클래스 : 움직이는 GIF 이미지 만들기 (0) | 2022.08.20 |
[C#/WPF] BlurEffect 엘리먼트 : 네온싸인 효과 만들기 (0) | 2022.07.23 |
[C#/WPF] DropShadowEffect 엘리먼트 : 네온싸인 효과 만들기 (0) | 2022.07.23 |
댓글을 달아 주세요