@@ -6,7 +6,6 @@ namespace DevWinUI;
6
6
public static partial class StartupHelper
7
7
{
8
8
public static string UnPackagedAppStartupTag { get ; } = "/onBoot" ;
9
- private static string PackagedAppTaskId { get ; set ; }
10
9
11
10
private static readonly string UnPackagedAppRegistryKey = ProcessInfoHelper . ProductName ;
12
11
@@ -41,71 +40,155 @@ public static bool IsAppStartupWithWindowsForXamlBindingEnabled
41
40
}
42
41
43
42
/// <summary>
44
- /// Set a TaskId for a packaged application.
43
+ /// Disables the application startup with Windows.
44
+ /// For Packaged applications, the taskId is automatically used from the first StartupTask defined in Package.appxmanifest file.
45
+ /// For UnPackaged applications, Registry.CurrentUser Will be used
45
46
/// </summary>
46
- /// <param name="taskId" ></param >
47
- public static void SetTaskIdForPackagedApp ( string taskId )
47
+ /// <returns ></returns >
48
+ public static async Task < bool > DisableAppStartupWithWindowsAsync ( )
48
49
{
49
- PackagedAppTaskId = taskId ;
50
+ return await SetStartupAsync ( false , null , Registry . CurrentUser ) ;
50
51
}
51
52
52
53
/// <summary>
53
54
/// Disables the application startup with Windows.
55
+ /// For Packaged applications, the taskId is automatically used from the first StartupTask defined in Package.appxmanifest file.
54
56
/// </summary>
57
+ /// <param name="registryKey">Define registry scope (CurrentUser or LocalMachine) for UnPackaged App</param>
55
58
/// <returns></returns>
56
- public static async Task < bool > DisableAppStartupWithWindowsAsync ( )
59
+ public static async Task < bool > DisableAppStartupWithWindowsAsync ( RegistryKey registryKey )
57
60
{
58
- return await SetStartupAsync ( false , Registry . CurrentUser ) ;
61
+ return await SetStartupAsync ( false , null , registryKey ) ;
59
62
}
60
63
61
64
/// <summary>
62
65
/// Disables the application startup with Windows.
66
+ /// For UnPackaged applications, Registry.CurrentUser Will be used
63
67
/// </summary>
68
+ /// <param name="taskId">Set a TaskId for a packaged application.</param>
69
+ /// <returns></returns>
70
+ public static async Task < bool > DisableAppStartupWithWindowsAsync ( string taskId )
71
+ {
72
+ return await SetStartupAsync ( false , taskId , Registry . CurrentUser ) ;
73
+ }
74
+
75
+ /// <summary>
76
+ /// Disables the application startup with Windows.
77
+ /// </summary>
78
+ /// <param name="taskId">Set a TaskId for a packaged application.</param>
64
79
/// <param name="registryKey">Define registry scope (CurrentUser or LocalMachine) for UnPackaged App</param>
65
80
/// <returns></returns>
66
- public static async Task < bool > DisableAppStartupWithWindowsAsync ( RegistryKey registryKey )
81
+ public static async Task < bool > DisableAppStartupWithWindowsAsync ( string taskId , RegistryKey registryKey )
67
82
{
68
- return await SetStartupAsync ( false , registryKey ) ;
83
+ return await SetStartupAsync ( false , taskId , registryKey ) ;
69
84
}
70
85
71
86
/// <summary>
72
87
/// Enables the application startup with Windows.
88
+ /// For Packaged applications, the taskId is automatically used from the first StartupTask defined in Package.appxmanifest file.
89
+ /// For UnPackaged applications, Registry.CurrentUser Will be used
73
90
/// </summary>
74
91
/// <returns></returns>
75
92
public static async Task < bool > EnableAppStartupWithWindowsAsync ( )
76
93
{
77
- return await SetStartupAsync ( true , Registry . CurrentUser ) ;
94
+ return await SetStartupAsync ( true , null , Registry . CurrentUser ) ;
78
95
}
79
96
80
97
/// <summary>
81
98
/// Enables the application startup with Windows.
99
+ /// For Packaged applications, the taskId is automatically used from the first StartupTask defined in Package.appxmanifest file.
82
100
/// </summary>
83
101
/// <param name="registryKey">Define registry scope (CurrentUser or LocalMachine) for UnPackaged App</param>
84
102
/// <returns></returns>
85
103
public static async Task < bool > EnableAppStartupWithWindowsAsync ( RegistryKey registryKey )
86
104
{
87
- return await SetStartupAsync ( true , registryKey ) ;
105
+ return await SetStartupAsync ( true , null , registryKey ) ;
106
+ }
107
+
108
+ /// <summary>
109
+ /// Enables the application startup with Windows.
110
+ /// For UnPackaged applications, Registry.CurrentUser Will be used
111
+ /// </summary>
112
+ /// <param name="taskId">Set a TaskId for a packaged application.</param>
113
+ /// <returns></returns>
114
+ public static async Task < bool > EnableAppStartupWithWindowsAsync ( string taskId )
115
+ {
116
+ return await SetStartupAsync ( true , taskId , Registry . CurrentUser ) ;
117
+ }
118
+
119
+ /// <summary>
120
+ /// Enables the application startup with Windows.
121
+ /// </summary>
122
+ /// <param name="taskId">Set a TaskId for a packaged application.</param>
123
+ /// <param name="registryKey">Define registry scope (CurrentUser or LocalMachine) for UnPackaged App</param>
124
+ /// <returns></returns>
125
+ public static async Task < bool > EnableAppStartupWithWindowsAsync ( string taskId , RegistryKey registryKey )
126
+ {
127
+ return await SetStartupAsync ( true , taskId , registryKey ) ;
88
128
}
89
129
90
130
/// <summary>
91
131
/// Checks if the application is configured to run at startup.
132
+ /// For Packaged applications, the taskId is automatically used from the first StartupTask defined in Package.appxmanifest file.
133
+ /// For UnPackaged applications, Registry.CurrentUser Will be used
92
134
/// </summary>
93
135
/// <returns></returns>
94
136
public static async Task < bool > IsAppStartupWithWindowsEnabledAsync ( )
95
137
{
96
- return await IsAppStartupWithWindowsEnabledAsync ( Registry . CurrentUser ) ;
138
+ return await IsAppStartupWithWindowsEnabledAsync ( null , Registry . CurrentUser ) ;
97
139
}
98
140
99
141
/// <summary>
100
142
/// Checks if the application is configured to run at startup.
143
+ /// For Packaged applications, the taskId is automatically used from the first StartupTask defined in Package.appxmanifest file.
101
144
/// </summary>
102
145
/// <param name="registryKey">Define registry scope (CurrentUser or LocalMachine) for UnPackaged App</param>
103
146
/// <returns></returns>
104
147
public static async Task < bool > IsAppStartupWithWindowsEnabledAsync ( RegistryKey registryKey )
148
+ {
149
+ return await IsAppStartupWithWindowsEnabledAsync ( null , registryKey ) ;
150
+ }
151
+
152
+ /// <summary>
153
+ /// Checks if the application is configured to run at startup.
154
+ /// For UnPackaged applications, Registry.CurrentUser Will be used
155
+ /// </summary>
156
+ /// <param name="taskId">Set a TaskId for a packaged application.</param>
157
+ /// <returns></returns>
158
+ public static async Task < bool > IsAppStartupWithWindowsEnabledAsync ( string taskId )
159
+ {
160
+ return await IsAppStartupWithWindowsEnabledAsync ( taskId , Registry . CurrentUser ) ;
161
+ }
162
+
163
+ /// <summary>
164
+ /// Checks if the application is configured to run at startup.
165
+ /// </summary>
166
+ /// <param name="taskId">Set a TaskId for a packaged application.</param>
167
+ /// <param name="registryKey">Define registry scope (CurrentUser or LocalMachine) for UnPackaged App</param>
168
+ /// <returns></returns>
169
+ public static async Task < bool > IsAppStartupWithWindowsEnabledAsync ( string taskId , RegistryKey registryKey )
105
170
{
106
171
if ( PackageHelper . IsPackaged )
107
172
{
108
- var startupTask = await StartupTask . GetAsync ( PackagedAppTaskId ) ;
173
+ StartupTask startupTask = null ;
174
+ if ( string . IsNullOrEmpty ( taskId ) )
175
+ {
176
+ var taskList = await StartupTask . GetForCurrentPackageAsync ( ) ;
177
+ if ( taskList . Count > 0 )
178
+ {
179
+ startupTask = taskList . FirstOrDefault ( ) ;
180
+ }
181
+ }
182
+ else
183
+ {
184
+ startupTask = await StartupTask . GetAsync ( taskId ) ;
185
+ }
186
+
187
+ if ( startupTask == null )
188
+ {
189
+ throw new Exception ( "Couldn't find a StartupTask in the appx manifest with the input taskId" ) ;
190
+ }
191
+
109
192
return startupTask . State == StartupTaskState . Enabled || startupTask . State == StartupTaskState . EnabledByPolicy ;
110
193
}
111
194
else
@@ -114,11 +197,29 @@ public static async Task<bool> IsAppStartupWithWindowsEnabledAsync(RegistryKey r
114
197
}
115
198
}
116
199
117
- private static async Task < bool > SetStartupAsync ( bool startup , RegistryKey registryKey )
200
+ private static async Task < bool > SetStartupAsync ( bool startup , string taskId , RegistryKey registryKey )
118
201
{
119
202
if ( PackageHelper . IsPackaged )
120
203
{
121
- var startupTask = await StartupTask . GetAsync ( PackagedAppTaskId ) ;
204
+ StartupTask startupTask = null ;
205
+ if ( string . IsNullOrEmpty ( taskId ) )
206
+ {
207
+ var taskList = await StartupTask . GetForCurrentPackageAsync ( ) ;
208
+ if ( taskList . Count > 0 )
209
+ {
210
+ startupTask = taskList . FirstOrDefault ( ) ;
211
+ }
212
+ }
213
+ else
214
+ {
215
+ startupTask = await StartupTask . GetAsync ( taskId ) ;
216
+ }
217
+
218
+ if ( startupTask == null )
219
+ {
220
+ throw new Exception ( "Couldn't find a StartupTask in the appx manifest with the input taskId" ) ;
221
+ }
222
+
122
223
switch ( startupTask . State )
123
224
{
124
225
case StartupTaskState . Disabled :
0 commit comments