|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.IO; |
| 4 | + |
| 5 | +namespace Downloader.Desktop.Services; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Windows: self-register a Start-menu shortcut on first run. winget's zip/portable install puts the |
| 9 | +/// exe on PATH but creates NO Start-menu entry — users reported "installed successfully but I can't |
| 10 | +/// find it anywhere". Idempotent (skips when the shortcut exists), best-effort, per-user (no admin). |
| 11 | +/// Removed by deleting %APPDATA%\Microsoft\Windows\Start Menu\Programs\Downloader.lnk. |
| 12 | +/// </summary> |
| 13 | +public static class StartMenuShortcut |
| 14 | +{ |
| 15 | + public static void EnsureOnWindows() |
| 16 | + { |
| 17 | + if (!OperatingSystem.IsWindows()) |
| 18 | + return; |
| 19 | + try |
| 20 | + { |
| 21 | + var exe = Environment.ProcessPath; |
| 22 | + if (string.IsNullOrWhiteSpace(exe)) |
| 23 | + return; |
| 24 | + var programs = Path.Combine( |
| 25 | + Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), |
| 26 | + "Microsoft", "Windows", "Start Menu", "Programs"); |
| 27 | + var lnk = Path.Combine(programs, "Downloader.lnk"); |
| 28 | + if (File.Exists(lnk)) |
| 29 | + return; |
| 30 | + Directory.CreateDirectory(programs); |
| 31 | + |
| 32 | + var script = BuildShortcutScript(lnk, exe); |
| 33 | + Process.Start(new ProcessStartInfo("powershell", |
| 34 | + $"-NoProfile -Command \"{script}\"") |
| 35 | + { |
| 36 | + UseShellExecute = false, |
| 37 | + CreateNoWindow = true |
| 38 | + }); |
| 39 | + } |
| 40 | + catch |
| 41 | + { |
| 42 | + // a missing shortcut must never break startup |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary>The PowerShell that creates the .lnk (WScript.Shell COM). Pure — unit-tested. |
| 47 | + /// The working dir is split on '\' explicitly so the helper behaves the same when the tests run |
| 48 | + /// it on Linux (Path.GetDirectoryName doesn't parse Windows paths there).</summary> |
| 49 | + internal static string BuildShortcutScript(string lnkPath, string exePath) |
| 50 | + { |
| 51 | + var cut = exePath.LastIndexOf('\\'); |
| 52 | + var workDir = cut > 0 ? exePath[..cut] : exePath; |
| 53 | + return "$s=(New-Object -ComObject WScript.Shell).CreateShortcut('" + lnkPath + "'); " + |
| 54 | + "$s.TargetPath='" + exePath + "'; " + |
| 55 | + "$s.WorkingDirectory='" + workDir + "'; " + |
| 56 | + "$s.Description='Downloader — fast multi-connection download manager'; " + |
| 57 | + "$s.Save()"; |
| 58 | + } |
| 59 | +} |
0 commit comments