diff --git a/Flow.Launcher/App.xaml.cs b/Flow.Launcher/App.xaml.cs index 969bb75bb..8c5915cd4 100644 --- a/Flow.Launcher/App.xaml.cs +++ b/Flow.Launcher/App.xaml.cs @@ -236,7 +236,7 @@ namespace Flow.Launcher { try { - Helper.AutoStartup.CheckIsEnabled(_settings.UseLogonTaskForStartup); + Helper.AutoStartup.CheckIsEnabled(_settings.UseLogonTaskForStartup, _settings.AlwaysRunAsAdministrator); } catch (Exception e) { diff --git a/Flow.Launcher/Helper/AutoStartup.cs b/Flow.Launcher/Helper/AutoStartup.cs index 34700c610..e6fd1ba1e 100644 --- a/Flow.Launcher/Helper/AutoStartup.cs +++ b/Flow.Launcher/Helper/AutoStartup.cs @@ -17,18 +17,18 @@ public class AutoStartup private const string LogonTaskName = $"{Constant.FlowLauncher} Startup"; private const string LogonTaskDesc = $"{Constant.FlowLauncher} Auto Startup"; - public static void CheckIsEnabled(bool useLogonTaskForStartup) + public static void CheckIsEnabled(bool useLogonTaskForStartup, bool alwaysRunAsAdministrator) { // We need to check both because if both of them are enabled, // Hide Flow Launcher on startup will not work since the later one will trigger main window show event - var logonTaskEnabled = CheckLogonTask(); + var logonTaskEnabled = CheckLogonTask(alwaysRunAsAdministrator); var registryEnabled = CheckRegistry(); if (useLogonTaskForStartup) { // Enable logon task if (!logonTaskEnabled) { - Enable(true); + Enable(true, alwaysRunAsAdministrator); } // Disable registry if (registryEnabled) @@ -41,7 +41,7 @@ public class AutoStartup // Enable registry if (!registryEnabled) { - Enable(false); + Enable(false, alwaysRunAsAdministrator); } // Disable logon task if (logonTaskEnabled) @@ -51,7 +51,7 @@ public class AutoStartup } } - private static bool CheckLogonTask() + private static bool CheckLogonTask(bool alwaysRunAsAdministrator) { using var taskService = new TaskService(); var task = taskService.RootFolder.AllTasks.FirstOrDefault(t => t.Name == LogonTaskName); @@ -67,7 +67,7 @@ public class AutoStartup if (!action.Equals(Constant.ExecutablePath, StringComparison.OrdinalIgnoreCase)) { UnscheduleLogonTask(); - ScheduleLogonTask(); + ScheduleLogonTask(alwaysRunAsAdministrator); } } @@ -117,16 +117,17 @@ public class AutoStartup Disable(false); } - public static void ChangeToViaLogonTask() + public static void ChangeToViaLogonTask(bool alwaysRunAsAdministrator) { Disable(false); - Enable(true); + Enable(true, alwaysRunAsAdministrator); } public static void ChangeToViaRegistry() { Disable(true); - Enable(false); + // We do not need to use alwaysRunAsAdministrator for registry, so we just set false here + Enable(false, false); } private static void Disable(bool logonTask) @@ -149,13 +150,13 @@ public class AutoStartup } } - private static void Enable(bool logonTask) + private static void Enable(bool logonTask, bool alwaysRunAsAdministrator) { try { if (logonTask) { - ScheduleLogonTask(); + ScheduleLogonTask(alwaysRunAsAdministrator); } else { @@ -169,14 +170,15 @@ public class AutoStartup } } - private static bool ScheduleLogonTask() + private static bool ScheduleLogonTask(bool alwaysRunAsAdministrator) { using var td = TaskService.Instance.NewTask(); td.RegistrationInfo.Description = LogonTaskDesc; td.Triggers.Add(new LogonTrigger { UserId = WindowsIdentity.GetCurrent().Name, Delay = TimeSpan.FromSeconds(2) }); td.Actions.Add(Constant.ExecutablePath); - if (IsCurrentUserIsAdmin()) + // Only if the app is running as administrator, we can set the run level to highest + if (Win32Helper.IsAdministrator() && alwaysRunAsAdministrator) { td.Principal.RunLevel = TaskRunLevel.Highest; } @@ -212,13 +214,6 @@ public class AutoStartup } } - private static bool IsCurrentUserIsAdmin() - { - var identity = WindowsIdentity.GetCurrent(); - var principal = new WindowsPrincipal(identity); - return principal.IsInRole(WindowsBuiltInRole.Administrator); - } - private static bool UnscheduleRegistry() { using var key = Registry.CurrentUser.OpenSubKey(StartupPath, true); diff --git a/Flow.Launcher/Resources/Pages/WelcomePage5.xaml.cs b/Flow.Launcher/Resources/Pages/WelcomePage5.xaml.cs index 8db0a9f7e..dc88f74fd 100644 --- a/Flow.Launcher/Resources/Pages/WelcomePage5.xaml.cs +++ b/Flow.Launcher/Resources/Pages/WelcomePage5.xaml.cs @@ -45,7 +45,7 @@ namespace Flow.Launcher.Resources.Pages { if (Settings.UseLogonTaskForStartup) { - AutoStartup.ChangeToViaLogonTask(); + AutoStartup.ChangeToViaLogonTask(Settings.AlwaysRunAsAdministrator); } else { diff --git a/Flow.Launcher/SettingPages/ViewModels/SettingsPaneGeneralViewModel.cs b/Flow.Launcher/SettingPages/ViewModels/SettingsPaneGeneralViewModel.cs index eac40099b..eed81fe17 100644 --- a/Flow.Launcher/SettingPages/ViewModels/SettingsPaneGeneralViewModel.cs +++ b/Flow.Launcher/SettingPages/ViewModels/SettingsPaneGeneralViewModel.cs @@ -49,7 +49,7 @@ public partial class SettingsPaneGeneralViewModel : BaseModel { if (UseLogonTaskForStartup) { - AutoStartup.ChangeToViaLogonTask(); + AutoStartup.ChangeToViaLogonTask(AlwaysRunAsAdministrator); } else { @@ -81,7 +81,7 @@ public partial class SettingsPaneGeneralViewModel : BaseModel { if (value) { - AutoStartup.ChangeToViaLogonTask(); + AutoStartup.ChangeToViaLogonTask(AlwaysRunAsAdministrator); } else {