diff --git a/src/App.xaml.cs b/src/App.xaml.cs index ec6c813..1a76ba7 100644 --- a/src/App.xaml.cs +++ b/src/App.xaml.cs @@ -282,25 +282,5 @@ private void ExitApplication() System.Windows.Application.Current.Shutdown(); } - public static void SetAutoStart(bool enable) - { - try - { - string path = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"; - using RegistryKey key = Registry.CurrentUser.OpenSubKey(path, true)!; - string exePath = System.Environment.ProcessPath ?? System.Diagnostics.Process.GetCurrentProcess().MainModule!.FileName; - - if (enable) - key.SetValue("BASpark", $"\"{exePath}\" --autostart"); - else - key.DeleteValue("BASpark", false); - - ConfigManager.Save("AutoStart", enable); - } - catch (Exception ex) - { - System.Windows.MessageBox.Show("自启设置失败: " + ex.Message); - } - } } } diff --git a/src/ControlPanelWindow.xaml.cs b/src/ControlPanelWindow.xaml.cs index dbbaafc..ddeb52e 100644 --- a/src/ControlPanelWindow.xaml.cs +++ b/src/ControlPanelWindow.xaml.cs @@ -1474,7 +1474,6 @@ private void SaveSettings_Click(object sender, RoutedEventArgs e) IsEnabled = item.IsEnabled })); - App.SetAutoStart(ConfigManager.AutoStart); ApplyAutoStartSettings(); App.Overlay?.UpdateColor(ConfigManager.ParticleColor); @@ -1596,23 +1595,29 @@ private void ApplyAutoStartSettings() try { + // 注册表 Run 条目(普通自启)与计划任务(管理员自启)是两条独立路径, + // 注册表打开失败不应阻止计划任务的创建/清理 using (RegistryKey? key = Registry.CurrentUser.CreateSubKey(regKeyPath, true)) { - if (plan.RegistryRunEnabled) + if (key == null) { - key?.SetValue(AutoStartManager.RunValueName, AutoStartManager.BuildRunCommand(exePath)); + AppLogger.Warn($"Failed to open registry key for auto-start: {regKeyPath}"); + } + else if (plan.RegistryRunEnabled) + { + key.SetValue(AutoStartManager.RunValueName, AutoStartManager.BuildRunCommand(exePath)); } else { - key?.DeleteValue(AutoStartManager.RunValueName, false); + key.DeleteValue(AutoStartManager.RunValueName, false); } - - ManageTaskScheduler(AutoStartManager.TaskName, exePath, plan.ScheduledTaskEnabled); } + + ManageTaskScheduler(AutoStartManager.TaskName, exePath, plan.ScheduledTaskEnabled); } catch (Exception ex) { - Debug.WriteLine("应用高权限自启失败: " + ex.Message); + AppLogger.Warn($"Failed to apply auto-start settings: {ex.Message}"); } }