Created
April 18, 2019 18:47
-
-
Save lski/108938d0427b9cdf795f0af65fc61350 to your computer and use it in GitHub Desktop.
Launch a web broswer with optional URL on Win/Mac/Linux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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