Files
OpenNest/OpenNest.Desktop/ExternalFileLauncher.cs
T
ajandClaude Opus 5.5 098e1d92a7 refactor(desktop): share plate export, file launching and nest report writing
Move the toolkit-neutral parts of File > Export/Export All, Plate > View
in CAD and the nest report commands into OpenNest.Desktop: export formats
and suggested per-plate file names (now free of characters any platform
refuses), the placement list, DXF/SVG writing with the view image left to
the front end, opening a file in its associated program on Windows, macOS
and Linux, the report file name check, capture with catalog density, the
machine's thickness name and saved cut-time settings, and viewed-report
temp folders. WinForms calls the shared code; its behaviour is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 19:27:07 -05:00

46 lines
1.9 KiB
C#

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace OpenNest.Desktop;
/// <summary>
/// Opens a file in the program the system associates with its type: Plate &gt; View in CAD's
/// DXF, View Nest Report's PDF. Windows opens it through the shell, macOS through
/// <c>open</c> and Linux and other Unix desktops through <c>xdg-open</c>.
/// </summary>
public static class ExternalFileLauncher
{
/// <summary>The start info that opens <paramref name="path"/> on the running platform.</summary>
public static ProcessStartInfo StartInfo(string path) => StartInfo(path, CurrentPlatform());
/// <summary>The start info that opens <paramref name="path"/> on <paramref name="platform"/>.</summary>
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;
}
/// <summary>Opens <paramref name="path"/> in its associated program; throws when it cannot start.</summary>
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;
}