Microsoft WebView2: How to check if the WebView2 runtime is installed
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 with throw a 'Microsofr.Web.WebView2.Core.WebView2RuntimeNotFoundException' exception. You can use a try catch to detect if this function fails and if it takes, 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.
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).