在 C# 中主要有两种方式来检测 Webview2 是否已经安装:
通过
CoreWebView2Environment.GetAvailableBrowserVersionString()
返回值来判断
通过注册表值来判断
这种方式不是很稳定,有时候用户的注册会缺失
GetAvailableBrowserVersionString()
方法
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 public static class WebView2Install { public static InstallInfo GetInfo () { var version = GetWebView2Version(); return new InstallInfo(version); } private static string GetWebView2Version () { try { return CoreWebView2Environment.GetAvailableBrowserVersionString(); } catch (Exception) { return "" ; } } } public class InstallInfo { public InstallInfo (string version ) => (Version) = (version); public string Version { get ; } public InstallType InstallType => Version switch { var version when version.Contains("dev" ) => InstallType.EdgeChromiumDev, var version when version.Contains("beta" ) => InstallType.EdgeChromiumBeta, var version when version.Contains("canary" ) => InstallType.EdgeChromiumCanary, var version when !string .IsNullOrEmpty(version) => InstallType.WebView2, _ => InstallType.NotInstalled }; } public enum InstallType{ WebView2, EdgeChromiumBeta, EdgeChromiumCanary, EdgeChromiumDev, NotInstalled }
注册表法
1 2 3 4 5 6 7 8 public static bool HasWebView2InstalledByReg (){ using var key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\WOW6432Node\Microsoft\EdgeUpdate\Clients\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}" ); if (key == null ) return false ; var versionStr = key.GetValue("pv" ); if (versionStr == null ) return false ; return !string .IsNullOrEmpty(versionStr.ToString()); }
参考
.net
- Detect if WebView2 is installed on clients machine (VB.NET) - Stack
Overflow