Apps running on startup is very common in Windows these days; many apps do this in order to run in the background to provide services such as notifications or a tray/taskbar corner icon. Windows has multiple avenues apps can use to run on startup however, the concept is usually the same - apps can specify an executable to run on startup along with arguments either for all users or the current user and the executable will run when the user signs in. One of these avenues is the StartupTask feature of UWP/MISX packages. You can simply specify these in your app's manifest and your app will be able to run when the current user signs in. As a developer, you might want to write code to check if your app was launched by a startup task - this might be useful if, for example, you want your app to run in the background and not show a main window if it is launched automatically from a StartupTask.
How to check if app was launched from a StartupTask
The first thing we'll need to do is make sure that we have access to the appropriate APIs that can be used to check how the app was launched. Unless your app is a UWP app (so for example if your app is a WPF (Windows Presentation Foundation) or WinForms/Windows Forms app), you may not be able to access these APIs by default. Below are instructions for how to access these APIs when using .Net 5 and above and for when using .Net Core 3.0, 3.1 or .Net Framework 4.5 or above as the instructions are different depending on what you use. Follow the instructions that match what .Net version you're using. If you've already got access to the Windows Runtime (WinRT)/UWP APIs (i.e. the ones in the Windows namespace) for Windows 10 1709 (build 16299) or above, then you can skip these steps.
For .Net 5 and above
If you're using .Net 5 or above, the first thing you'll need to do is to make sure your project is set the target a version of Windows 10 that is 1709 (build 16299) or above. To do this, right click the project and select 'Edit Project File'. This will open your project file in text view. From here, you'll need to specify a target Windows 10 version in the
TargetFramework element under the PropertyGroup element in the format of [.netversion]-windows[windowsversionnumber] as shown in the code below.
The code below specifies Windows 10 version 1903 (The May 2019 update) and .Net 7. Which version you specify depends on compatibility - for best compatibility, specify the lowest version of Windows 10 you want your app to run on as this will give you access to APIs that are compatible with that version of Windows and above. To see the specific build numbers for each major release of Windows 10, see
here and
here for Windows 11 if you want to specify a Windows 11 version instead. The code in this article has been tested with 10.0.18362.0 and above being specified in the target framework. This may seem a little complicated, but it's necessary in .Net 5 and above in order to access the Windows 10 UWP/RT APIs, which we'll be using. If you're confused, we'd recommend just copying and pasting the below code into your project file under the PropertyGroup section, replacing the
TargetFramework element if it already exists and making sure that 'net7.0' is set to the version of .Net you want to target. With this version of Windows specified, your code should work fine on Windows 10 May 2019 Update and above, so long as the specified version of .Net is compatible with it (which
.Net 7 is).
<TargetFramework>net7.0-windows10.0.18362.0</TargetFramework>
What you'll end up with, is something like this (note that this is quite a basic example, you might have more in this file depending on your project):
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net7.0-windows10.0.18362.0</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
</PropertyGroup>
</Project>
Now, we can access the APIs we need.
For .Net Framework 4.5 and above and .Net Core 3.0 and 3.1
If you're using .Net Framework 4.5 or above or .Net Core 3.1, follow this instructions here. Note that these instructions aren't compatible with older versions of .Net Framework or .Net Core. So first, we'll need to installed the
Microsoft.Windows.SDK.Contracts NuGet package to access the UWP/WinRT APIs. To do this, right click on your project and select 'Manage NuGet Packages...' then go to the Browse tab, search for 'Microsoft.Windows.SDK.Contracts' and install
Microsoft.Windows.SDK.Contracts for Windows 10 version 1709 (build 16299) or above. Which version you select depends on compatibility - for best compatibility, select the version that matches lowest version of Windows 10 you want your app to run on as this will give you access to APIs that are compatible with that version of Windows and above. The lowest supported version of Windows that each version of this package is supported on is listed in the description. For this tutorial, we'll be using version 10.0.18362.2005 which is compatible with Windows 10 1903 (May 2019 update) but you can use another version if you want. To see the specific build numbers for each major release of Windows 10, see
here and
here for Windows 11. If you're unsure, just use the same version we're using and your code should work fine on Windows 10 1903 and above.
Now, we can access the APIs we need.
Checking how the app was launched
Now that we have access to the APIs we need, checking how the app was launched is pretty easy. To check how the app was launched, in order to see if it was launched from a StartupTask, we can use the
ApplicationModel.AppInstance.GetActivatedEventArgs function to get the information about the app's launch, then we can check the
Kind property to see how the app was launched. We can do this like so:
C#:
if (Windows.ApplicationModel.AppInstance.GetActivatedEventArgs().Kind == Windows.ApplicationModel.Activation.ActivationKind.StartupTask)
{
// App was launched automatically from a StartupTask
}
VB:
If Windows.ApplicationModel.AppInstance.GetActivatedEventArgs.Kind = Windows.ApplicationModel.Activation.ActivationKind.StartupTask Then
' App was launched automatically from a StartupTask
End If
This snippet is available in Codly. Click the download link below to download the snippet. If you don't have Codly, it is available here in the Microsoft Store. Did this guide help you? Do you have anything to add? Do you have any questions? Let us know in the comments! Also, if this guide did help you and you use an AdBlocker and you want to support us, consider disabling it for this site - if you do, it will be greatly appreciated!
Follow us to be notified when we release new guides:
Comments
Post a Comment