Skip to content
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. #17938

Closed
gerardog opened this issue Jul 27, 2016 · 14 comments
Closed

How to start the system default browser. #17938

gerardog opened this issue Jul 27, 2016 · 14 comments
Labels
area-System.Diagnostics.Process bug help wanted [up-for-grabs] Good issue for external contributors
Milestone

Comments

@gerardog
Copy link

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
Copy link
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.

@akoeplinger
Copy link
Member

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

@stephentoub
Copy link
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

@brockallen
Copy link

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
Copy link
Contributor

Priya91 commented Dec 7, 2016

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

@Priya91 Priya91 closed this as completed Dec 7, 2016
@stephentoub
Copy link
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
Copy link
Contributor

Priya91 commented Dec 7, 2016

Leaving the issue open as a bug for investigation.

@nowherenearithaca
Copy link

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

@danmoseley
Copy link
Member

This should work now (with useshellexecute=true) on Windows and work on Unix after https://github.com/dotnet/corefx/issues/19956

@danmoseley
Copy link
Member

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

@j717273419
Copy link

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

It's worked,thanks.

@danmoseley
Copy link
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?

@CodeMyst
Copy link

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.

bitbonk referenced this issue in bitbonk/website Oct 4, 2019
- On .NET Core 3.0 on Windows you will get a Win32Exception when you try to open any URL, because of this https://github.com/dotnet/corefx/issues/10361
- The proposed change was tested on .NET Core on Windows but should work anywhere.
glennawatson referenced this issue in reactiveui/website Oct 4, 2019
- On .NET Core 3.0 on Windows you will get a Win32Exception when you try to open any URL, because of this https://github.com/dotnet/corefx/issues/10361
- The proposed change was tested on .NET Core on Windows but should work anywhere.
@HebaruSan
Copy link

I'm seeing this now on Linux when trying Process.Start("https://www.github.com/"), and my investigation brought me to this issue:

System.ComponentModel.Win32Exception (0x80004005): Cannot find the specified file
  at System.Diagnostics.Process.StartWithShellExecuteEx (System.Diagnostics.ProcessStartInfo startInfo) [0x00102] in <dfe42095fd31410e95d8021f755c3557>:0 
  at System.Diagnostics.Process.Start () [0x00032] in <dfe42095fd31410e95d8021f755c3557>:0 
  at (wrapper remoting-invoke-with-check) System.Diagnostics.Process.Start()
  at System.Diagnostics.Process.Start (System.Diagnostics.ProcessStartInfo startInfo) [0x0001b] in <dfe42095fd31410e95d8021f755c3557>:0 

However, I'm confused because I'm running Mono and this used to work, so I must have installed or upgraded something at some point that exposed me to this (maybe Mono can use corefx libraries?). I've been trying to catch up on the network of related issues and pull requests to assess the impact, but it's a slog. Can anyone please summarize the scope of this issue, i.e. who is and is not affected by it?

If I want my app to work on all of the available platforms and runtimes, do I need to implement a permanent workaround for launching URLs?

Sejsel added a commit to gw2scratch/evtc that referenced this issue Jul 26, 2020
.NET Core changes the default value of UseShellExecute when launching processes.

dotnet/runtime#17938
@ghost ghost locked as resolved and limited conversation to collaborators Dec 30, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Diagnostics.Process bug help wanted [up-for-grabs] Good issue for external contributors
Projects
None yet
Development

No branches or pull requests