using System.IO.Compression; using System.IO; namespace LumaTunnel.Client.Installer; internal static class InstallerActions { private static readonly string InstallDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Programs", "LumaTunnel"); private static string Executable => Path.Combine(InstallDirectory, "LumaTunnel.exe"); public static async Task InstallAsync(bool desktopShortcut) { var payload = Path.Combine(AppContext.BaseDirectory, "LumaTunnel.Client.zip"); if (!File.Exists(payload)) throw new FileNotFoundException("安装负载 LumaTunnel.Client.zip 不存在。", payload); Directory.CreateDirectory(InstallDirectory); await Task.Run(() => ZipFile.ExtractToDirectory(payload, InstallDirectory, true)).ConfigureAwait(false); if (!File.Exists(Executable)) throw new InvalidDataException("安装负载中缺少 LumaTunnel.exe。"); var programs = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu), "Programs", "LumaTunnel.lnk"); CreateShortcut(programs, Executable); if (desktopShortcut) CreateShortcut(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "LumaTunnel.lnk"), Executable); return Executable; } public static async Task UninstallAsync() { try { DeleteShortcut(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu), "Programs", "LumaTunnel.lnk")); DeleteShortcut(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "LumaTunnel.lnk")); if (Directory.Exists(InstallDirectory)) await Task.Run(() => Directory.Delete(InstallDirectory, true)).ConfigureAwait(false); return true; } catch { return false; } } private static void CreateShortcut(string shortcutPath, string targetPath) { var type = Type.GetTypeFromProgID("WScript.Shell") ?? throw new PlatformNotSupportedException("Windows Script Host is unavailable."); dynamic shell = Activator.CreateInstance(type)!; dynamic shortcut = shell.CreateShortcut(shortcutPath); shortcut.TargetPath = targetPath; shortcut.WorkingDirectory = InstallDirectory; shortcut.IconLocation = targetPath + ",0"; shortcut.Description = "LumaTunnel 光隧"; shortcut.Save(); System.Runtime.InteropServices.Marshal.FinalReleaseComObject(shortcut); System.Runtime.InteropServices.Marshal.FinalReleaseComObject(shell); } private static void DeleteShortcut(string path) { if (File.Exists(path)) File.Delete(path); } }