Microsoft WebView2: How to check if the WebView2 runtime is installed in C# and VB

With the initial release of Windows 10, Microsoft released a completely new web browser - Microsoft Edge - that used its own rendering engine - EdgeHTML. In recent years, Microsoft deprecated the original version of Microsoft Edge and replaced it with a Chromium based version. The original version of Microsoft Edge came with a control called WebView which could be used by apps to display content. Now that Microsoft has released the new Chromium based Edge, a new control has been released called WebView2. The existing WebView control still uses the original Microsoft Edge whereas the new WebView2 uses the Chromium based Microsoft Edge. The new control however, is not reliant on the same version of Microsoft Edge that is used as the main browser - instead it is reliant on a separate component known as the WebView2 Runtime (except in the case of non-stable channels, which come with the WebView2 runtime). This component is preinstalled in Windows 11 and is currently being rolled out to Windows 10. If the runtime is not installed, the WebView2 control will not work. In the case of the WinUI 2 and 3 WebView2 controls, an error message with a link to install the runtime will be displayed instead. Because of this, you might want to distribute the WebView2 runtime with your app if it uses WebView2. Unfortunately, this may not be possible - for example, if your app is distributed via the Microsoft Store. If you can't guarantee that the WebView2 runtime is installed, you may want to check that it is installed and with a) Revert back to another control (such as WebView or the Internet Explorer based control) or b) take some other action such as showing an error message or automatically downloading the runtime.

Checking if the WebView2 runtime is installed

The WebView2 includes the following function: 'Microsoft.Web.WebView2.Core.CoreWebView2Environment.GetAvailableBrowserVersionString()' that retrieves information about the currently installed version of the WebView2 runtime. If there is no version of the Edge WebView2 runtime installed, however, this function will throw a 'Microsofr.Web.WebView2.Core.WebView2RuntimeNotFoundException' exception. You can use a try catch to detect if this function fails and if it does, take appropriate action such as falling back to the Edge WebView or the Internet Explorer based control. Here are some examples in C# and VB.








    // WebView2 runtime is not installed or there is some other issue with it






    ' WebView2 runtime is not installed or there is some other issue with it

End Try

Tip: You can also use this function to check if a certain version of the WebView2 runtime is installed (or a certain or greater version).

This snippet is available in Codly. Click the appropriate link below to download the snippet. If you don't have Codly, it is available here in the Microsoft Store.


Popular posts from this blog

How to show placeholder text in a WPF TextBox in C# and VB

How to enable dark title bar in WPF and Windows Forms/WinForms apps in C# and VB

How to use modern icons in XAML in WPF on Windows 10 and 11

How to change the colour of a WPF or Windows Forms/WinForms title bar in Windows 11 in C# and VB