diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WebSearch/Helpers/Browser/BrowserInfoServiceExtensions.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WebSearch/Helpers/Browser/BrowserInfoServiceExtensions.cs index 1614273d83..40777dd391 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WebSearch/Helpers/Browser/BrowserInfoServiceExtensions.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WebSearch/Helpers/Browser/BrowserInfoServiceExtensions.cs @@ -2,6 +2,8 @@ // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System; +using ManagedCommon; using Microsoft.CommandPalette.Extensions.Toolkit; namespace Microsoft.CmdPal.Ext.WebSearch.Helpers.Browser; @@ -26,6 +28,22 @@ internal static class BrowserInfoServiceExtensions /// public static bool Open(this IBrowserInfoService browserInfoService, string url) { + // If the URL is a valid URI, attempt to open it with the default browser by invoking it through the shell. + if (Uri.TryCreate(url, UriKind.Absolute, out _)) + { + try + { + ShellHelpers.OpenInShell(url); + return true; + } + catch (Exception ex) + { + Logger.LogDebug($"Failed to launch the URI {url}: {ex}"); + } + } + + // Use legacy method to open the URL if it's not a well-formed URI or if the shell launch fails. + // This may handle cases where the URL is a search query or a custom URI scheme. var defaultBrowser = browserInfoService.GetDefaultBrowser(); return defaultBrowser != null && ShellHelpers.OpenCommandInShell(defaultBrowser.Path, defaultBrowser.ArgumentsPattern, url); }