mirror of
https://github.com/Flow-Launcher/Flow.Launcher.git
synced 2026-03-11 08:54:32 +00:00
Add admin support for auto startup
This commit is contained in:
parent
d29a5d4b1f
commit
40cf413898
4 changed files with 19 additions and 24 deletions
|
|
@ -236,7 +236,7 @@ namespace Flow.Launcher
|
|||
{
|
||||
try
|
||||
{
|
||||
Helper.AutoStartup.CheckIsEnabled(_settings.UseLogonTaskForStartup);
|
||||
Helper.AutoStartup.CheckIsEnabled(_settings.UseLogonTaskForStartup, _settings.AlwaysRunAsAdministrator);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ namespace Flow.Launcher.Resources.Pages
|
|||
{
|
||||
if (Settings.UseLogonTaskForStartup)
|
||||
{
|
||||
AutoStartup.ChangeToViaLogonTask();
|
||||
AutoStartup.ChangeToViaLogonTask(Settings.AlwaysRunAsAdministrator);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue