11using System ;
2- using System . IO ;
32using System . Linq ;
43using System . Security . Principal ;
54using Flow . Launcher . Infrastructure ;
65using Microsoft . Win32 ;
76using Microsoft . Win32 . TaskScheduler ;
87
8+ #nullable enable
9+
910namespace Flow . Launcher . Helper ;
1011
1112public class AutoStartup
@@ -59,18 +60,23 @@ private static bool CheckLogonTask()
5960 try
6061 {
6162 // Check if the action is the same as the current executable path
62- var action = task . Definition . Actions . FirstOrDefault ( ) ! . ToString ( ) . Trim ( ) ;
63- if ( ! Constant . ExecutablePath . Equals ( action , StringComparison . OrdinalIgnoreCase ) && ! File . Exists ( action ) )
63+ // If not, we need to unschedule and reschedule the task
64+ if ( task . Definition . Actions . FirstOrDefault ( ) is Microsoft . Win32 . TaskScheduler . Action taskAction )
6465 {
65- UnscheduleLogonTask ( ) ;
66- ScheduleLogonTask ( ) ;
66+ var action = taskAction . ToString ( ) . Trim ( ) ;
67+ if ( ! action . Equals ( Constant . ExecutablePath , StringComparison . OrdinalIgnoreCase ) )
68+ {
69+ UnscheduleLogonTask ( ) ;
70+ ScheduleLogonTask ( ) ;
71+ }
6772 }
6873
6974 return true ;
7075 }
7176 catch ( Exception e )
7277 {
7378 App . API . LogError ( ClassName , $ "Failed to check logon task: { e } ") ;
79+ throw ; // Throw exception so that App.AutoStartup can show error message
7480 }
7581 }
7682
@@ -82,15 +88,27 @@ private static bool CheckRegistry()
8288 try
8389 {
8490 using var key = Registry . CurrentUser . OpenSubKey ( StartupPath , true ) ;
85- var path = key ? . GetValue ( Constant . FlowLauncher ) as string ;
86- return path == Constant . ExecutablePath ;
91+ if ( key != null )
92+ {
93+ // Check if the action is the same as the current executable path
94+ // If not, we need to unschedule and reschedule the task
95+ var action = ( key . GetValue ( Constant . FlowLauncher ) as string ) ?? string . Empty ;
96+ if ( ! action . Equals ( Constant . ExecutablePath , StringComparison . OrdinalIgnoreCase ) )
97+ {
98+ UnscheduleRegistry ( ) ;
99+ ScheduleRegistry ( ) ;
100+ }
101+
102+ return true ;
103+ }
104+
105+ return false ;
87106 }
88107 catch ( Exception e )
89108 {
90- App . API . LogError ( ClassName , $ "Ignoring non-critical registry error (querying if enabled): { e } ") ;
109+ App . API . LogError ( ClassName , $ "Failed to check registry: { e } ") ;
110+ throw ; // Throw exception so that App.AutoStartup can show error message
91111 }
92-
93- return false ;
94112 }
95113
96114 public static void DisableViaLogonTaskAndRegistry ( )
@@ -121,8 +139,7 @@ private static void Disable(bool logonTask)
121139 }
122140 else
123141 {
124- using var key = Registry . CurrentUser . OpenSubKey ( StartupPath , true ) ;
125- key ? . DeleteValue ( Constant . FlowLauncher , false ) ;
142+ UnscheduleRegistry ( ) ;
126143 }
127144 }
128145 catch ( Exception e )
@@ -142,8 +159,7 @@ private static void Enable(bool logonTask)
142159 }
143160 else
144161 {
145- using var key = Registry . CurrentUser . OpenSubKey ( StartupPath , true ) ;
146- key ? . SetValue ( Constant . FlowLauncher , $ "\" { Constant . ExecutablePath } \" ") ;
162+ ScheduleRegistry ( ) ;
147163 }
148164 }
149165 catch ( Exception e )
@@ -202,4 +218,18 @@ private static bool IsCurrentUserIsAdmin()
202218 var principal = new WindowsPrincipal ( identity ) ;
203219 return principal . IsInRole ( WindowsBuiltInRole . Administrator ) ;
204220 }
221+
222+ private static bool UnscheduleRegistry ( )
223+ {
224+ using var key = Registry . CurrentUser . OpenSubKey ( StartupPath , true ) ;
225+ key ? . DeleteValue ( Constant . FlowLauncher , false ) ;
226+ return true ;
227+ }
228+
229+ private static bool ScheduleRegistry ( )
230+ {
231+ using var key = Registry . CurrentUser . OpenSubKey ( StartupPath , true ) ;
232+ key ? . SetValue ( Constant . FlowLauncher , $ "\" { Constant . ExecutablePath } \" ") ;
233+ return true ;
234+ }
205235}
0 commit comments