Launch UWP App Via CommandLine ( Windows 10 Fall Creators Update Features )

This article will discuss how to launch the UWP app via command line arguments.

Win32 Application

We can launch the Win32 application via command line arguments. For example, open the command line application, type notepad. Notepad application will open. Let's see how to implement this feature into our UWP application.

Note

This API will support Windows Insider Build version 16266 or greater, public version will be available October 17.  

System Requirements

  • Windows 10 Insider Build 16266 or greater
  • Windows 10 16266 SDK or greater
  • Visual Studio 2017

Steps to Implements

Create a sample UWP app

Open VisualStudio -> File -> Visual C# -> Windows Universal -> Blank App (Universal Windows), Ex: Project Name: CommandLineUWP

Open Package.appmainfest in XML editor

 

Add the namespace in the top of the file 
  1. xmlns:uap5="http://schemas.microsoft.com/appx/manifest/uap/windows10/5"  
Add the Extensions blog into the Application area
  1. <Extensions>  
  2.         <uap5:Extension   
  3.           Category="windows.appExecutionAlias"   
  4.           Executable="CommandLineUWP.exe"   
  5.           EntryPoint="CommandLineUWP.App">  
  6.           <uap5:AppExecutionAlias>  
  7.             <uap5:ExecutionAlias Alias="CommandLineUWP.exe" />  
  8.           </uap5:AppExecutionAlias>  
  9.         </uap5:Extension>  
  10.       </Extensions>  

Category name should be “windows.appExecutionAlias”,

  • Executable name - Application.Exe name (Ex: CommandLineUWP.exe)
  • EntryPoint - Entrypoint tag should contain the Full name of the application class (Ref: open -> App.xaml file -> Application x: Class name)
  • AppExecutionAlias - Type alias name in command prompt to open the application; give the app.exe name as alias name .
Note

If alias name conflicts with another installed app, installed app will be prioritized and your app won’t open. B
 
Build the application and run it one time
 
Open CommandPrompt
 
Open commandPrompt , type the alias name -> Enter
 
 
 
Pass the arguments via CommandLine
 
To Launch the application via command prompt, OnActivated function gets invoked in the application, here we can read the command line arguments,

First check if the ActivationKind is commandLineLanuch , then convert the IActivatedEventArgs as a CommandLineActivatedEventArgs. In CommandLineActivatedEventArgs class contains the CommandLineActivationOperation class object. 
  1. protected override void OnActivated(IActivatedEventArgs args)  
  2.         {  
  3.             string argment = "OnActivated";  
  4.             if (args.Kind == ActivationKind.CommandLineLaunch)  
  5.             {  
  6.                 var commandLine = args as CommandLineActivatedEventArgs;  
  7.                 if (commandLine != null)  
  8.                 {  
  9.                     var operation = commandLine.Operation;  
  10.                     CStatus.Status = operation.Arguments;  
  11.                 }  
  12.             }  
  13.   
  14.             Frame rootFrame = Window.Current.Content as Frame;  
  15.   
  16.             // Do not repeat app initialization when the Window already has content,  
  17.             // just ensure that the window is active  
  18.             if (rootFrame == null)  
  19.             {  
  20.                 // Create a Frame to act as the navigation context and navigate to the first page  
  21.                 rootFrame = new Frame();  
  22.   
  23.                 rootFrame.NavigationFailed += OnNavigationFailed;  
  24.                 // Place the frame in the current Window  
  25.                 Window.Current.Content = rootFrame;  
  26.             }  
  27.   
  28.             rootFrame.Navigate(typeof(MainPage), argment);  
  29.               
  30.             Window.Current.Activate();  
  31.             base.OnActivated(args);  
  32.         }  
Build the application and run one time

Go to command prompt; type the file name and pass the arguments

Output 

 

Conclusion

I hope you understood how to run the application via CommandPrompt.

Up Next
    Ebook Download
    View all
    Learn
    View all