Open a browser with a specific URL by Console application
15,474
Solution 1
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;
}
}
}
Solution 2
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);
}
}
}
Author by
user2091010
Updated on June 07, 2022Comments
-
user2091010 about 2 years
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
-
Lyall over 8 yearsYou can simplify all of the Main into a single line:
System.Diagnostics.Process.Start(@"http://URL");
-
Evgeny Gorbovoy over 2 yearsOutOfMemoryException or FileNotFoundException does not mean we should try platform specific code. catch probably should have Win32Exception ? But also which codes..