using System; using System.Diagnostics; using System.Runtime.InteropServices; namespace OpenNest.Desktop; /// /// Opens a file in the program the system associates with its type: Plate > View in CAD's /// DXF, View Nest Report's PDF. Windows opens it through the shell, macOS through /// open and Linux and other Unix desktops through xdg-open. /// public static class ExternalFileLauncher { /// The start info that opens on the running platform. public static ProcessStartInfo StartInfo(string path) => StartInfo(path, CurrentPlatform()); /// The start info that opens on . public static ProcessStartInfo StartInfo(string path, OSPlatform platform) { ArgumentException.ThrowIfNullOrWhiteSpace(path); // .NET defaults UseShellExecute to false, which would try to run the file itself as a // program; the shell opens it in the associated program instead. if (platform == OSPlatform.Windows) return new ProcessStartInfo(path) { UseShellExecute = true }; var info = new ProcessStartInfo(platform == OSPlatform.OSX ? "open" : "xdg-open") { UseShellExecute = false, }; // An argument, never a shell command line: spaces and quotes in the path stay literal. info.ArgumentList.Add(path); return info; } /// Opens in its associated program; throws when it cannot start. public static void Open(string path) { using var process = Process.Start(StartInfo(path)); } private static OSPlatform CurrentPlatform() => RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? OSPlatform.Windows : RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? OSPlatform.OSX : OSPlatform.Linux; }