using System.Diagnostics;
using System.IO;
using Microsoft.Win32;
namespace ClassNotify.Setup.Services;
public class StartupManager
{
private const string RunKeyPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
private const string ValueName = "ClassNotifyClient";
///
/// P1 Batch 8 Hardening: Service is the sole authorized launcher of Client with dynamic IPC token.
/// Cleans up any legacy autorun entries in HKLM/HKCU Run and scheduled tasks to prevent un-tokened clients from starting.
///
public static void CleanupLegacyStartupEntries(Action? onProgress = null)
{
onProgress?.Invoke("清理遗留开机自启注册表与计划任务...");
try
{
using var key = Registry.LocalMachine.OpenSubKey(RunKeyPath, writable: true);
key?.DeleteValue(ValueName, throwOnMissingValue: false);
}
catch { }
try
{
using var cuKey = Registry.CurrentUser.OpenSubKey(RunKeyPath, writable: true);
cuKey?.DeleteValue(ValueName, throwOnMissingValue: false);
}
catch { }
string[] legacyTasks = { ValueName, "ClassNotify_Client_UI", "ClassNotify" };
foreach (var task in legacyTasks)
{
try
{
var psi = new ProcessStartInfo
{
FileName = "schtasks.exe",
Arguments = $"/delete /tn \"{task}\" /f",
CreateNoWindow = true,
UseShellExecute = false
};
using var proc = Process.Start(psi);
proc?.WaitForExit(3000);
}
catch { }
}
}
public static void DisableStartup()
{
CleanupLegacyStartupEntries(null);
}
public static void TerminateClient()
{
try
{
var processes = Process.GetProcessesByName("ClassNotify.Client");
foreach (var p in processes)
{
try
{
p.Kill();
p.WaitForExit(3000);
}
catch { }
}
}
catch { }
}
}