Skip to content

Instantly share code, notes, and snippets.

@lski
Created April 18, 2019 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lski/108938d0427b9cdf795f0af65fc61350 to your computer and use it in GitHub Desktop.
Save lski/108938d0427b9cdf795f0af65fc61350 to your computer and use it in GitHub Desktop.
Launch a web broswer with optional URL on Win/Mac/Linux
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace WebBrowser
{
public static class Browser
{
public static Process Launch(string url, string browser = null)
{
Process process = null;
if (!string.IsNullOrEmpty(url))
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
process = Process.Start(new ProcessStartInfo("cmd", $"/c start \"\" {browser} {url}"));
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
process = Process.Start(browser ?? "xdg-open", url); // Not tested
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
process = Process.Start("open", url); // Not tested as I dont own a mac
}
}
return process ?? null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment