Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Process.Start("microsoft-edge:") throws Win32Exception in dot net core

When I execute the following line of code:

Process.Start("microsoft-edge:");

Or

Process.Start("microsoft-edge:http://localhost");

It gives me this error:

System.ComponentModel.Win32Exception: 'The system cannot find the file specified.'

When I run microsoft-edge: using Win+R it works.

When I run the same code in .net framework it works.

I'm using .netcore 3.0.0-preview6-27804-01

Any idea why this is happening?


Edit:

These are not working either:

Process.Start(@"c:\Windows\System32\LaunchWinApp.exe:http://localhost");
Process.Start(@"http://localhost");

All other executables on my system work.

Also this is working too but I can't open a specific webpage with it:

Process.Start("explorer", @"shell:Appsfolder\Microsoft.MicrosoftEdge_8wekyb3d8bbwe!MicrosoftEdge");
like image 578
Bizhan Avatar asked Jul 16 '19 12:07

Bizhan


People also ask

How do I start edge maximization?

Enable Maximized Mode. First, create a desktop shortcut for Microsoft Edge. Then right-click on the Edge shortcut and select Properties. Click on the Shortcut tab, go to Run and use the drop-down menu to set the option to Maximized. Save the changes and check the results.

What is System diagnostics process start?

Starts a process resource by specifying the name of an application and a set of command-line arguments, and associates the resource with a new Process component. public: static System::Diagnostics::Process ^ Start(System::String ^ fileName, System::String ^ arguments); C# Copy. public static System.Diagnostics.


3 Answers

You cannot simply open a url with the expected call Process.Start("url");

You have to create a ProcessStartInfo and pass your browser and url as arguments:

Process.Start(new ProcessStartInfo("cmd", $"/c start microsoft-edge:http://localhost") { CreateNoWindow = true });

(edit) The use of ProcessStartInfo is required because we need to set its CreateNoWindow = true to prevent cmd window from showing up.

But as this code not only can be run on a Windows machine, i'd consider using something more cross-platform specific like this (https://brockallen.com/2016/09/24/process-start-for-urls-on-net-core/):

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

And call it with OpenBrowser("http://localhost");

like image 74
M. Schena Avatar answered Oct 15 '22 18:10

M. Schena


The exact root cause is indicated here: https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.useshellexecute?view=netframework-4.8

"The default is true on .NET Framework apps and false on .NET Core apps."

This means the direct solution is to set UseShellExecute. The following is basically the same as the original except making an explicit Process object to modify a setting.

       Process pWeb = new Process();
       pWeb.StartInfo.UseShellExecute = true;
       pWeb.StartInfo.FileName = "microsoft-edge:http://localhost";
       pWeb.Start();

Credit to @Lex Li and @Bizhan for their comments leading to this solution.

like image 44
Alan Baljeu Avatar answered Oct 15 '22 20:10

Alan Baljeu


This variation seems to do the job as well:

Process.Start(new ProcessStartInfo("msedge") { 
  UseShellExecute = true, 
  Arguments = "http://localhost" });

It avoids the strangeness of the microsoft-edge:<url> syntax thus allowing for more chrome arguments to be passed in the usual way.

If anyone knows of a reason to avoid this, feel free to speak up :)

like image 32
LOAS Avatar answered Oct 15 '22 20:10

LOAS