Skip to content

Commit

Permalink
Merge pull request #24 from vijayganeshpk/cross-platform-open-url
Browse files Browse the repository at this point in the history
fix: Cross-platform way to open url in default browser
  • Loading branch information
amusleh-spotware-com authored Sep 22, 2022
2 parents f303d50 + 07cc21e commit 054c6d6
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions samples/Console.Sample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Google.Protobuf;
using Google.Protobuf;
using OpenAPI.Net;
using OpenAPI.Net.Auth;
using OpenAPI.Net.Helpers;
Expand Down Expand Up @@ -85,7 +85,7 @@ private static async Task Main()

Console.WriteLine($"Authentication URI: {authUri}");

System.Diagnostics.Process.Start("explorer.exe", $"\"{authUri}\"");
OpenBrowser(authUri);

Console.WriteLine("Follow the authentication steps on your browser, then copy the authentication code from redirect" +
" URL and paste it here.");
Expand Down Expand Up @@ -489,5 +489,32 @@ private static void Disconnect()

Environment.Exit(0);
}

public static void OpenBrowser(Uri url)
{
try
{
System.Diagnostics.Process.Start(url.AbsoluteUri);
}
catch
{
if (System.OperatingSystem.IsWindows())
{
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("cmd", $"/c start {url.AbsoluteUri}") { CreateNoWindow = true });
}
else if (System.OperatingSystem.IsLinux())
{
System.Diagnostics.Process.Start("xdg-open", url.AbsoluteUri);
}
else if (System.OperatingSystem.IsMacOS())
{
System.Diagnostics.Process.Start("open", url.AbsoluteUri);
}
else
{
throw;
}
}
}
}
}

0 comments on commit 054c6d6

Please sign in to comment.