Skip to content

Commit 114e101

Browse files
committed
Update StartupHelper.cs
1 parent 9e73433 commit 114e101

File tree

1 file changed

+116
-15
lines changed

1 file changed

+116
-15
lines changed

dev/DevWinUI/Helpers/StartupHelper.cs

Lines changed: 116 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ namespace DevWinUI;
66
public static partial class StartupHelper
77
{
88
public static string UnPackagedAppStartupTag { get;} = "/onBoot";
9-
private static string PackagedAppTaskId { get; set; }
109

1110
private static readonly string UnPackagedAppRegistryKey = ProcessInfoHelper.ProductName;
1211

@@ -41,71 +40,155 @@ public static bool IsAppStartupWithWindowsForXamlBindingEnabled
4140
}
4241

4342
/// <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
4546
/// </summary>
46-
/// <param name="taskId"></param>
47-
public static void SetTaskIdForPackagedApp(string taskId)
47+
/// <returns></returns>
48+
public static async Task<bool> DisableAppStartupWithWindowsAsync()
4849
{
49-
PackagedAppTaskId = taskId;
50+
return await SetStartupAsync(false, null, Registry.CurrentUser);
5051
}
5152

5253
/// <summary>
5354
/// Disables the application startup with Windows.
55+
/// For Packaged applications, the taskId is automatically used from the first StartupTask defined in Package.appxmanifest file.
5456
/// </summary>
57+
/// <param name="registryKey">Define registry scope (CurrentUser or LocalMachine) for UnPackaged App</param>
5558
/// <returns></returns>
56-
public static async Task<bool> DisableAppStartupWithWindowsAsync()
59+
public static async Task<bool> DisableAppStartupWithWindowsAsync(RegistryKey registryKey)
5760
{
58-
return await SetStartupAsync(false, Registry.CurrentUser);
61+
return await SetStartupAsync(false, null, registryKey);
5962
}
6063

6164
/// <summary>
6265
/// Disables the application startup with Windows.
66+
/// For UnPackaged applications, Registry.CurrentUser Will be used
6367
/// </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>
6479
/// <param name="registryKey">Define registry scope (CurrentUser or LocalMachine) for UnPackaged App</param>
6580
/// <returns></returns>
66-
public static async Task<bool> DisableAppStartupWithWindowsAsync(RegistryKey registryKey)
81+
public static async Task<bool> DisableAppStartupWithWindowsAsync(string taskId, RegistryKey registryKey)
6782
{
68-
return await SetStartupAsync(false, registryKey);
83+
return await SetStartupAsync(false, taskId, registryKey);
6984
}
7085

7186
/// <summary>
7287
/// 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
7390
/// </summary>
7491
/// <returns></returns>
7592
public static async Task<bool> EnableAppStartupWithWindowsAsync()
7693
{
77-
return await SetStartupAsync(true, Registry.CurrentUser);
94+
return await SetStartupAsync(true, null, Registry.CurrentUser);
7895
}
7996

8097
/// <summary>
8198
/// Enables the application startup with Windows.
99+
/// For Packaged applications, the taskId is automatically used from the first StartupTask defined in Package.appxmanifest file.
82100
/// </summary>
83101
/// <param name="registryKey">Define registry scope (CurrentUser or LocalMachine) for UnPackaged App</param>
84102
/// <returns></returns>
85103
public static async Task<bool> EnableAppStartupWithWindowsAsync(RegistryKey registryKey)
86104
{
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);
88128
}
89129

90130
/// <summary>
91131
/// 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
92134
/// </summary>
93135
/// <returns></returns>
94136
public static async Task<bool> IsAppStartupWithWindowsEnabledAsync()
95137
{
96-
return await IsAppStartupWithWindowsEnabledAsync(Registry.CurrentUser);
138+
return await IsAppStartupWithWindowsEnabledAsync(null, Registry.CurrentUser);
97139
}
98140

99141
/// <summary>
100142
/// 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.
101144
/// </summary>
102145
/// <param name="registryKey">Define registry scope (CurrentUser or LocalMachine) for UnPackaged App</param>
103146
/// <returns></returns>
104147
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)
105170
{
106171
if (PackageHelper.IsPackaged)
107172
{
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+
109192
return startupTask.State == StartupTaskState.Enabled || startupTask.State == StartupTaskState.EnabledByPolicy;
110193
}
111194
else
@@ -114,11 +197,29 @@ public static async Task<bool> IsAppStartupWithWindowsEnabledAsync(RegistryKey r
114197
}
115198
}
116199

117-
private static async Task<bool> SetStartupAsync(bool startup, RegistryKey registryKey)
200+
private static async Task<bool> SetStartupAsync(bool startup, string taskId, RegistryKey registryKey)
118201
{
119202
if (PackageHelper.IsPackaged)
120203
{
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+
122223
switch (startupTask.State)
123224
{
124225
case StartupTaskState.Disabled:

0 commit comments

Comments
 (0)