Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions src/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
19 changes: 12 additions & 7 deletions src/ControlPanelWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1474,7 +1474,6 @@
IsEnabled = item.IsEnabled
}));

App.SetAutoStart(ConfigManager.AutoStart);
ApplyAutoStartSettings();

App.Overlay?.UpdateColor(ConfigManager.ParticleColor);
Expand Down Expand Up @@ -1586,7 +1585,7 @@
string? exePath = AutoStartManager.ResolveExecutablePath(
Environment.ProcessPath,
Process.GetCurrentProcess().MainModule?.FileName,
Assembly.GetExecutingAssembly().Location,

Check warning on line 1588 in src/ControlPanelWindow.xaml.cs

View workflow job for this annotation

GitHub Actions / build

'System.Reflection.Assembly.Location' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'.
AppDomain.CurrentDomain.BaseDirectory);

if (string.IsNullOrEmpty(exePath)) return;
Expand All @@ -1596,23 +1595,29 @@

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}");
}
}

Expand Down
Loading