How to start the system default browser. #10361

Open
gerardog opened this Issue Jul 27, 2016 · 8 comments

Projects

None yet

9 participants

@gerardog

The following line doesn't work:
Process.Start("http://localhost:5000");

Microsoft said that that was the official way to start a new browser, a long time ago, at:
https://support.microsoft.com/en-us/kb/305703

Is there any multiplatform way to do this now? Will it ever be?

@mellinoe
Member

There's not a single call that will work on all platforms, but you can use something like this:

public static void OpenBrowser(string url)
{
    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
    {
        Process.Start(new ProcessStartInfo("cmd", $"/c start {url}"));
    }
    else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
    {
        Process.Start("xdg-open", url);
    }
    else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
    {
        Process.Start("open", url);
    }
    else
    {
        ...
    }
}

I haven't tried the OSX call above, but it seems to do what you want according to a google search. I've used the other two and they work as expected.

@akoeplinger
Member

Process.Start("https://www.google.com"); does work on Mono though.

@stephentoub
Member

I believe the main issue here is that UseShellExecute defaults to true in desktop (and presumably Mono, though I haven't verified), but false in corefx. It's false by default because the APIs necessary to support true on Windows aren't currently available in all target Windows platforms. It can be set to true on Unix. We should revisit this.
cc: @danmosemsft

@joshfree joshfree added this to the 1.2.0 milestone Aug 2, 2016
@joshfree joshfree assigned Priya91 and unassigned danmosemsft Aug 2, 2016
@brockallen
brockallen commented Sep 24, 2016 edited

Slight improvement on what @mellinoe suggested:

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
    url = url.Replace("&", "^&");
    Process.Start(new ProcessStartInfo("cmd", $"/c start {url}") { CreateNoWindow = true });
}
@Priya91
Member
Priya91 commented Dec 7, 2016

@gerardog Closing this issue, please reopen if the suggested methods don't work for you.

@Priya91 Priya91 closed this Dec 7, 2016
@stephentoub
Member

Why are we closing this? Just because there's a workaround doesn't mean it's addressed. There's functionality that works in desktop and works in mono but doesn't in core: that's something to investigate further. Have we already done so?

@stephentoub stephentoub reopened this Dec 7, 2016
@Priya91
Member
Priya91 commented Dec 7, 2016

Leaving the issue open as a bug for investigation.

@Priya91 Priya91 added the up for grabs label Dec 7, 2016
@Priya91 Priya91 modified the milestone: Future, 1.2.0 Dec 7, 2016
@Priya91 Priya91 was unassigned by karelz Dec 8, 2016
@nowherenearithaca

FYI - this seemed to work fine on mac for me - El Capitan 10.11.6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment