From c35eca27887f1b5811bc393818046aea1c3274ec Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Fri, 25 Apr 2025 10:22:36 +0800 Subject: [PATCH] Fix logon task issue message box & Improve startup path check --- Flow.Launcher/Helper/AutoStartup.cs | 58 ++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 14 deletions(-) diff --git a/Flow.Launcher/Helper/AutoStartup.cs b/Flow.Launcher/Helper/AutoStartup.cs index b83bb5948..34700c610 100644 --- a/Flow.Launcher/Helper/AutoStartup.cs +++ b/Flow.Launcher/Helper/AutoStartup.cs @@ -1,11 +1,12 @@ using System; -using System.IO; using System.Linq; using System.Security.Principal; using Flow.Launcher.Infrastructure; using Microsoft.Win32; using Microsoft.Win32.TaskScheduler; +#nullable enable + namespace Flow.Launcher.Helper; public class AutoStartup @@ -59,11 +60,15 @@ public class AutoStartup try { // Check if the action is the same as the current executable path - var action = task.Definition.Actions.FirstOrDefault()!.ToString().Trim(); - if (!Constant.ExecutablePath.Equals(action, StringComparison.OrdinalIgnoreCase) && !File.Exists(action)) + // If not, we need to unschedule and reschedule the task + if (task.Definition.Actions.FirstOrDefault() is Microsoft.Win32.TaskScheduler.Action taskAction) { - UnscheduleLogonTask(); - ScheduleLogonTask(); + var action = taskAction.ToString().Trim(); + if (!action.Equals(Constant.ExecutablePath, StringComparison.OrdinalIgnoreCase)) + { + UnscheduleLogonTask(); + ScheduleLogonTask(); + } } return true; @@ -71,6 +76,7 @@ public class AutoStartup catch (Exception e) { App.API.LogError(ClassName, $"Failed to check logon task: {e}"); + throw; // Throw exception so that App.AutoStartup can show error message } } @@ -82,15 +88,27 @@ public class AutoStartup try { using var key = Registry.CurrentUser.OpenSubKey(StartupPath, true); - var path = key?.GetValue(Constant.FlowLauncher) as string; - return path == Constant.ExecutablePath; + if (key != null) + { + // Check if the action is the same as the current executable path + // If not, we need to unschedule and reschedule the task + var action = (key.GetValue(Constant.FlowLauncher) as string) ?? string.Empty; + if (!action.Equals(Constant.ExecutablePath, StringComparison.OrdinalIgnoreCase)) + { + UnscheduleRegistry(); + ScheduleRegistry(); + } + + return true; + } + + return false; } catch (Exception e) { - App.API.LogError(ClassName, $"Ignoring non-critical registry error (querying if enabled): {e}"); + App.API.LogError(ClassName, $"Failed to check registry: {e}"); + throw; // Throw exception so that App.AutoStartup can show error message } - - return false; } public static void DisableViaLogonTaskAndRegistry() @@ -121,8 +139,7 @@ public class AutoStartup } else { - using var key = Registry.CurrentUser.OpenSubKey(StartupPath, true); - key?.DeleteValue(Constant.FlowLauncher, false); + UnscheduleRegistry(); } } catch (Exception e) @@ -142,8 +159,7 @@ public class AutoStartup } else { - using var key = Registry.CurrentUser.OpenSubKey(StartupPath, true); - key?.SetValue(Constant.FlowLauncher, $"\"{Constant.ExecutablePath}\""); + ScheduleRegistry(); } } catch (Exception e) @@ -202,4 +218,18 @@ public class AutoStartup var principal = new WindowsPrincipal(identity); return principal.IsInRole(WindowsBuiltInRole.Administrator); } + + private static bool UnscheduleRegistry() + { + using var key = Registry.CurrentUser.OpenSubKey(StartupPath, true); + key?.DeleteValue(Constant.FlowLauncher, false); + return true; + } + + private static bool ScheduleRegistry() + { + using var key = Registry.CurrentUser.OpenSubKey(StartupPath, true); + key?.SetValue(Constant.FlowLauncher, $"\"{Constant.ExecutablePath}\""); + return true; + } }