Skip to content
Advertisement

Process.Start with .NET Core 2.0 on Linux

I am trying to start the default web browser open to a link with Process.Start(). I use Process.Start("https://www.google.com"), but my .NET Core App crashes with the following:

Exception has occurred: CLR/System.ComponentModel.Win32Exception An unhandled exception of type ‘System.ComponentModel.Win32Exception’ occurred in System.Diagnostics.Process.dll: ‘No such file or directory’ at System.Diagnostics.Process.ResolvePath(String filename) at System.Diagnostics.Process.StartCore(ProcessStartInfo startInfo) at System.Diagnostics.Process.Start() at System.Diagnostics.Process.Start(ProcessStartInfo startInfo) at VPGameHelper.Program.Main(String[] args) in /home/jj/VPGameHelper/Program.cs:line 30

Advertisement

Answer

I think this might be what you are looking for:

Process.Start for URLs on .NET Core

The original question was asked here where.

public static void OpenBrowser(string url)
{
    try
    {
        Process.Start(url);
    }
    catch
    {
        // hack because of this: https://github.com/dotnet/corefx/issues/10361
        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
        {
            url = url.Replace("&", "^&");
            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;
        }
    }
}

Cheers and happy coding!

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement