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
- xmlns:uap5="http://schemas.microsoft.com/appx/manifest/uap/windows10/5"
Add the Extensions blog into the Application area
- <Extensions>
- <uap5:Extension
- Category="windows.appExecutionAlias"
- Executable="CommandLineUWP.exe"
- EntryPoint="CommandLineUWP.App">
- <uap5:AppExecutionAlias>
- <uap5:ExecutionAlias Alias="CommandLineUWP.exe" />
- </uap5:AppExecutionAlias>
- </uap5:Extension>
- </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.
- protected override void OnActivated(IActivatedEventArgs args)
- {
- string argment = "OnActivated";
- if (args.Kind == ActivationKind.CommandLineLaunch)
- {
- var commandLine = args as CommandLineActivatedEventArgs;
- if (commandLine != null)
- {
- var operation = commandLine.Operation;
- CStatus.Status = operation.Arguments;
- }
- }
-
- Frame rootFrame = Window.Current.Content as Frame;
-
-
-
- if (rootFrame == null)
- {
-
- rootFrame = new Frame();
-
- rootFrame.NavigationFailed += OnNavigationFailed;
-
- Window.Current.Content = rootFrame;
- }
-
- rootFrame.Navigate(typeof(MainPage), argment);
-
- Window.Current.Activate();
- base.OnActivated(args);
- }
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.