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
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;
}
}
}
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);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With