Search code examples
c#wpfuwpstartup

Check if a packaged WPF Application ran with startup or manually?


I have a WPF application that is packaged for being able to submit it in the Microsoft Store. I've used StartupTask extension to run it with system startup.

Package.appmanifest (in the Package project of the solution):

<Extensions>
  <uap5:Extension Category="windows.startupTask" Executable="*PathToExecutable*" EntryPoint="Windows.FullTrustApplication">
    <uap5:StartupTask TaskId="MyAppStartupTaskId" Enabled="true" DisplayName="Title in TaskManager" />
  </uap5:Extension>
</Extensions>

The problem is that there is no Argument property to set when running with startup, something like this:

<uap5:StartupTask Argument="/autostart" ... />

Then I could do something like this in the Application_Startup event:

App.xaml.cs (in the Application project of the solution):

private void Application_Startup(object sender, StartupEventArgs e)
{
    // Check if application is running by startup
    if (e.Args.Length > 0 && e.Args[0] == "/autostart")
    {
        LaunchMinimized = true;
    }
    // Lots of bla bla blas here...
}

The question is can I check if a program is launched with system startup in a WPF application with this StartupTask thing? Does system startup launcher attach any event or property to my app or its launch args to check for startup?


Solution

  • I am afraid you can't specify a parameter for a startup task, but if you are targeting Windows 10 version 18.03 or later you can use the AppInstance.GetActivatedEventArgs method to get an IActivatedEventArgs and then check its Kind property to determine whether the application was activated from a startup task.

    The other option would be to launch a different .EXE from the start up task.

    Those APIs are coming from Windows.ApplicationModel.dll and Windows.dll assemblies that you can't add them to your WPF app

    Sure you can. Make sure you have installed the latest SDK and add a reference to C:\Program Files (x86)\Windows Kits\10\References\10.0.17134.0\Windows.Foundation.UniversalApiContract\6.0.0.0\Windows.Foundation.UniversalApiContract.winmd.

    Then you can call the method like this in your WPF app:

    Windows.ApplicationModel.Activation.IActivatedEventArgs args = Windows.ApplicationModel.AppInstance.GetActivatedEventArgs();