1

I am trying to open a URL in a new browser window. Specifically, a new browser window. The following code launches the URL in the default browser, but always opens the URL as a new tab in an existing browser window (if one exists). I want to launch the new tab in a new browser window, regardless of if one (or more) browser windows are already open.

This code launches the URL in WinUI 3 (in a new tab in an existing browser window), and appears to be the simplest:

Windows.System.Launcher.LaunchUriAsync(new Uri("http://google.com"));

This code does the same thing, and also works in WinUI 3 (credit to this answer here):

Process myProcess = new Process();
myProcess.StartInfo.UseShellExecute = true; 
myProcess.StartInfo.FileName = "http://google.com";
myProcess.Start();

Please note that the following code does NOT work at all in WinUI 3:

System.Diagnostics.Process.Start("http://google.com");

EDIT: Additional Ruled-out Method

The following modification of Process.Start() works in WinUI 3, but like the above methods only opens new tabs in existing browser windows, and thus is not a solution to the question above (also note that it forces using a specific browser, rather than the default browser):

Process.Start("C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe", "http://google.com");
  • Whether a new tab or window is opened depends on how the default browser is implemented or configured...which browser are you using? The solution is not necessary the same for all browsers. – mm8 Jan 27 '22 at 19:53

2 Answers2

0

You can do it by calling a JavaScript command into webview2:

await webView.ExecuteScriptAsync("window.open('http://www.bing.com');");

Make sure you have either set the source of webView, or that you have already called EnsureCoreWebView2Async().

Nick
  • 4,787
  • 2
  • 18
  • 24
0
_ = await Windows.System.Launcher.LaunchUriAsync(new Uri("http://fakeurl.tld"));

Should be handled by whichever program is supposed to handle HTTP URIs

divay pandey
  • 152
  • 4
  • 8