Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a browser with a specific URL by Console application

I'm doing a console application in Visual Studio, but I have a little problem. If I want to open a browser with a specified URL when any key is pressed, how can I do that?

Thanks

like image 819
user2091010 Avatar asked Feb 20 '13 14:02

user2091010


2 Answers

If you want to cover also .Net Core applications.Thanks to Brock Allen

https://brockallen.com/2016/09/24/process-start-for-urls-on-net-core/

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;
        }
    }
}
like image 195
czlatea Avatar answered Oct 23 '22 02:10

czlatea


Use the ProcessStartInfo class instance to set of values that are used to start a process.

Something like this:

using System;
using System.Diagnostics;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            var psi = new ProcessStartInfo("iexplore.exe");
            psi.Arguments = "http://www.google.com/";
            Process.Start(psi);
        }
    }
}
like image 26
Grigorii Chudnov Avatar answered Oct 23 '22 02:10

Grigorii Chudnov