1
1
using System ;
2
- using System . IO ;
3
2
using System . Linq ;
4
3
using System . Security . Principal ;
5
4
using Flow . Launcher . Infrastructure ;
6
5
using Microsoft . Win32 ;
7
6
using Microsoft . Win32 . TaskScheduler ;
8
7
8
+ #nullable enable
9
+
9
10
namespace Flow . Launcher . Helper ;
10
11
11
12
public class AutoStartup
@@ -59,18 +60,23 @@ private static bool CheckLogonTask()
59
60
try
60
61
{
61
62
// 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 )
64
65
{
65
- UnscheduleLogonTask ( ) ;
66
- ScheduleLogonTask ( ) ;
66
+ var action = taskAction . ToString ( ) . Trim ( ) ;
67
+ if ( ! action . Equals ( Constant . ExecutablePath , StringComparison . OrdinalIgnoreCase ) )
68
+ {
69
+ UnscheduleLogonTask ( ) ;
70
+ ScheduleLogonTask ( ) ;
71
+ }
67
72
}
68
73
69
74
return true ;
70
75
}
71
76
catch ( Exception e )
72
77
{
73
78
App . API . LogError ( ClassName , $ "Failed to check logon task: { e } ") ;
79
+ throw ; // Throw exception so that App.AutoStartup can show error message
74
80
}
75
81
}
76
82
@@ -82,15 +88,27 @@ private static bool CheckRegistry()
82
88
try
83
89
{
84
90
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 ;
87
106
}
88
107
catch ( Exception e )
89
108
{
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
91
111
}
92
-
93
- return false ;
94
112
}
95
113
96
114
public static void DisableViaLogonTaskAndRegistry ( )
@@ -121,8 +139,7 @@ private static void Disable(bool logonTask)
121
139
}
122
140
else
123
141
{
124
- using var key = Registry . CurrentUser . OpenSubKey ( StartupPath , true ) ;
125
- key ? . DeleteValue ( Constant . FlowLauncher , false ) ;
142
+ UnscheduleRegistry ( ) ;
126
143
}
127
144
}
128
145
catch ( Exception e )
@@ -142,8 +159,7 @@ private static void Enable(bool logonTask)
142
159
}
143
160
else
144
161
{
145
- using var key = Registry . CurrentUser . OpenSubKey ( StartupPath , true ) ;
146
- key ? . SetValue ( Constant . FlowLauncher , $ "\" { Constant . ExecutablePath } \" ") ;
162
+ ScheduleRegistry ( ) ;
147
163
}
148
164
}
149
165
catch ( Exception e )
@@ -202,4 +218,18 @@ private static bool IsCurrentUserIsAdmin()
202
218
var principal = new WindowsPrincipal ( identity ) ;
203
219
return principal . IsInRole ( WindowsBuiltInRole . Administrator ) ;
204
220
}
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
+ }
205
235
}
0 commit comments