Skip to content

Instantly share code, notes, and snippets.

@RickStrahl
Created November 9, 2023 22:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RickStrahl/bd112952d4e83806aac9bc3d97191492 to your computer and use it in GitHub Desktop.
Save RickStrahl/bd112952d4e83806aac9bc3d97191492 to your computer and use it in GitHub Desktop.
Interactive UI version of whether WebView is installed and then launching installer with Admin Rights
/// <summary>
/// Helper function that checks to see if the WebView control is installed
/// and if not prompts to install it.
///
/// Should be called during app startup to ensure the WebView Runtime is available.
/// </summary>
/// <param name="showDownloadUi"></param>
/// <returns></returns>
public static bool IsWebViewVersionInstalledUi(bool showDownloadUi = false)
{
string versionNo = null;
Version asmVersion = null;
Version ver = null;
try
{
// If version no lookup or parsing fails WebView is not available
versionNo = CoreWebView2Environment.GetAvailableBrowserVersionString();
// strip off 'canary' or 'stable' version
versionNo = versionNo.ExtractString("", " ", allowMissingEndDelimiter: true)?.Trim();
ver = new Version(versionNo);
// DEBUG: Force a version mismatch
// ver = new Version(112, 0, 1600, 1);
if (!mmApp.Configuration.System.IgnoreWebViewVersionMismatch)
{
asmVersion = typeof(CoreWebView2Environment).Assembly.GetName().Version;
if (ver.Build >= asmVersion.Build)
return true;
}
else
{
return true;
}
}
catch(Exception ex)
{
mmApp.Log($"WebView Version lookup failed. {versionNo} {asmVersion}", ex.GetBaseException(), logLevel: LogLevels.Error);
}
if (!showDownloadUi)
return false;
var result = MessageBoxResult.None;
string title;
string msg;
if (string.IsNullOrEmpty(versionNo))
{
title = "WebView Runtime is not installed";
msg = @"The WebView Runtime is not installed.
In order to use Markdown Monster you need to install the latest Microsoft WebView runtime.
We'll shut down after you made a selection. Please restart Markdown Monster when done.
Do you want to install the Edge WebView Runtime now?";
}
else
{
title = "WebView Runtime is out of Date";
msg = $@"The WebView Runtime is older than the WebView component:
Installed Runtime Build: {ver.Build}
Required Runtime Build: {asmVersion.Build}
Do you want to want to update your installation now or continue with the outdated version?
Note: you can override this behavior by setting this configuration value to `true`:
System.IgnoreWebViewVersionMismatch=true
";
}
Task.Run(() =>
{
result = MessageBox.Show(
msg, title, MessageBoxButton.YesNo, MessageBoxImage.Information);
});
while (result == MessageBoxResult.None)
{
Thread.Sleep(100);
}
if (result == MessageBoxResult.Yes)
{
string path = System.IO.Path.Combine(App.InitialStartDirectory,
"BinSupport\\MicrosoftEdgeWebview2Setup.exe");
try
{
ShellUtils.ExecuteCommandLine("\"" + path +"\"", verb: "RunAs");
Dispatcher.CurrentDispatcher.Delay(3000, () => Environment.Exit(0));
}
catch(Exception ex)
{
mmApp.Log("Failed to run WebViewRuntime Installer: " + path, ex, logLevel: LogLevels.Error);
ShellUtils.OpenUrl(mmApp.Urls.WebViewInstallerUrl);
}
return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment