New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to start the system default browser. #10361

Closed
gerardog opened this Issue Jul 27, 2016 · 13 comments

Comments

Projects
None yet
@gerardog

gerardog commented Jul 27, 2016

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

This comment has been minimized.

Show comment
Hide comment
@mellinoe

mellinoe Jul 27, 2016

Contributor

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.

Contributor

mellinoe commented Jul 27, 2016

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

This comment has been minimized.

Show comment
Hide comment
@akoeplinger

akoeplinger Jul 27, 2016

Member

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

Member

akoeplinger commented Jul 27, 2016

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

@stephentoub

This comment has been minimized.

Show comment
Hide comment
@stephentoub

stephentoub Jul 27, 2016

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

Member

stephentoub commented Jul 27, 2016

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

@brockallen

This comment has been minimized.

Show comment
Hide comment
@brockallen

brockallen Sep 24, 2016

Slight improvement on what @mellinoe suggested:

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

brockallen commented Sep 24, 2016

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

This comment has been minimized.

Show comment
Hide comment
@Priya91

Priya91 Dec 7, 2016

Member

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

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

This comment has been minimized.

Show comment
Hide comment
@stephentoub

stephentoub Dec 7, 2016

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?

Member

stephentoub commented Dec 7, 2016

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

This comment has been minimized.

Show comment
Hide comment
@Priya91

Priya91 Dec 7, 2016

Member

Leaving the issue open as a bug for investigation.

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 milestones: Future, 1.2.0 Dec 7, 2016

@nowherenearithaca

This comment has been minimized.

Show comment
Hide comment
@nowherenearithaca

nowherenearithaca Dec 26, 2016

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

nowherenearithaca commented Dec 26, 2016

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

@danmosemsft

This comment has been minimized.

Show comment
Hide comment
@danmosemsft

danmosemsft Jul 26, 2017

Member

This should work now (with useshellexecute=true) on Windows and work on Unix after #19956

Member

danmosemsft commented Jul 26, 2017

This should work now (with useshellexecute=true) on Windows and work on Unix after #19956

@danmosemsft

This comment has been minimized.

Show comment
Hide comment
@danmosemsft

danmosemsft Sep 10, 2017

Member

I'm going to close as it's fixed/covered elsewhere.

Member

danmosemsft commented Sep 10, 2017

I'm going to close as it's fixed/covered elsewhere.

@karelz karelz modified the milestones: Future, 2.1.0 Oct 28, 2017

@j717273419

This comment has been minimized.

Show comment
Hide comment
@j717273419

j717273419 Apr 12, 2018

            ProcessStartInfo psi = new ProcessStartInfo
            {
                FileName = "cmd",
                Arguments = "/c start https://www.baidu.com/s?wd=beijing%20time"
            };
            Process.Start(psi);

It's worked,thanks.

j717273419 commented Apr 12, 2018

            ProcessStartInfo psi = new ProcessStartInfo
            {
                FileName = "cmd",
                Arguments = "/c start https://www.baidu.com/s?wd=beijing%20time"
            };
            Process.Start(psi);

It's worked,thanks.

@danmosemsft

This comment has been minimized.

Show comment
Hide comment
@danmosemsft

danmosemsft Apr 12, 2018

Member

You should not need to involve cmd.exe. If you set filename to the url and psi.useshellexecute =true it shoukd work. Including on Linux. Can you confirm?

Member

danmosemsft commented Apr 12, 2018

You should not need to involve cmd.exe. If you set filename to the url and psi.useshellexecute =true it shoukd work. Including on Linux. Can you confirm?

@CodeMyst

This comment has been minimized.

Show comment
Hide comment
@CodeMyst

CodeMyst May 20, 2018

ProcessStartInfo psi = new ProcessStartInfo
{
    FileName = url,
    UseShellExecute = true
};
Process.Start (psi);

This does work on Windows but not on Linux. In Linux I can simply do:

Process.Start ("xdg-open", url);

which works.

CodeMyst commented May 20, 2018

ProcessStartInfo psi = new ProcessStartInfo
{
    FileName = url,
    UseShellExecute = true
};
Process.Start (psi);

This does work on Windows but not on Linux. In Linux I can simply do:

Process.Start ("xdg-open", url);

which works.

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