One new feature introduced in the April 2018 Windows 10 update is the possibility of writing console apps using UWP. You could ask “why build an UWP console app if we have console apps that work fine now?”.

There are many advantages in using a UWP console app:

  • You can add them to the store and get all the benefits of them: easy discovery and deploy, virus verifying and monetizing possibilities
  • You have fast install and uninstall
  • You can use the new OS features with no hacks
  • You have a global access in the machine without having to add them to the path, like normal console apps

Writing the app

To start writing the app, you should have the April 2018 update and its SDK installed. Once you have that set up, you can open Visual Studio and, in Tools/Extensions and Updates install the Console Project template:

ConsoleTemplate

When the template is installed, you can create a new project and select the console app under the Universal tab:

NewConsoleApp

When you select it, you should set the minimum and target version to 1803, so it can run ok. Then, Visual Studio creates a console app similar to the ones you are used to, but with some twists:

  • There is an Assets folder with the assets for the tiles
  • There is a package.appxmanifest file, like any other UWP program

When you run it, it will open a console and write the data to it:

ConsoleApp

Until now, there’s nothing special. Things start to happen when you open the Start menu and see the icon for the app and you can add the tile to the start menu by right clicking the icon and selecting Pin to Start:

StartMenu

The other change you can see is that you can open a Command Prompt window and type ToastConsole param1 param2 (the name I have given to the app is ToastConsole) and the app will run, no matter in which folder you are. Pretty cool, no?

This app is an UWP app and it can be uninstalled like any other app. Once you uninstall it, it will be completely removed from your machine.

Adding OS features to the app

The cool feature for UWP Console apps is the possibility to add OS features to it and use them as we would do in a normal UWP app. We will add a toast notification to the app, it will take the parameters typed to the app and send a toast notification with the first parameter as the title and the second parameter as the content.

To create a toast notification, you should create the content as XML and use it to send the toast, but there are better ways to do it: you can install the Windows UWP Toolkit Notifications package and use its helper functions to create the toast content. Right click in the References node in the Solution Explorer and select Manage NuGet packages and install the Microsoft.Toolkit.Uwp.Notifications.

With it installed, you can create the ShowToast method:

static void ShowToast(string title, string content)
{
    ToastVisual visual = new ToastVisual
    {
        BindingGeneric = new ToastBindingGeneric
        {
            Children =
            {
                new AdaptiveText
                {
                    Text = title
                },

                new AdaptiveText
                {
                    Text = content
                }
            }
        }
    };
    ToastContent toastContent = new ToastContent
    {
        Visual = visual,
    };

    var toast = new ToastNotification(toastContent.GetXml());
    ToastNotificationManager.CreateToastNotifier().Show(toast);
}

You create a ToastVisual, then a ToastContent and a ToastNotification with the ToastContent as XML, then show the toast. The main program is similar to this:

static void Main(string[] args)
{
    var title = "Hello UWP Console";
    var content = "Sample toast launched from UWP Console app";
    if (args.Length >= 2)
    {
        title = args[0];
        content = args[1];
    }
    else if (args.Length == 1)
    {
        content = args[0];
    }
    ShowToast(title,content);
    Console.WriteLine("Press a key to continue: ");
    Console.ReadLine();
}

This code will take the two parameters passed to the command line and will create a new toast with them and will show the toast. Simple, no?

Toast

Until now, we’ve accessed the program with the name of the app, but we can change this. Just right click the package.appxmanifest file in the Solution Explorer and select View Code. There, you can edit the execution alias and change the Alias attribute:

<Extensions>
  <uap5:Extension 
    Category="windows.appExecutionAlias" 
    Executable="ToastConsole.exe" 
    EntryPoint="ToastConsole.Program">
    <uap5:AppExecutionAlias desktop4:Subsystem="console" iot2:Subsystem="console">
      <uap5:ExecutionAlias Alias="ToastSender.exe" />
    </uap5:AppExecutionAlias>
  </uap5:Extension>
</Extensions>

With this change, you will access the program with the ToastSender alias and ToastConsole won’t be acessible anymore.

This new resource opens a new kind of applications, now you have this option to create apps that interact with the OS with no need of an UI.

The source code for the application is in https://github.com/bsonnino/ToastConsole

2 Thoughts on “Writing console apps with UWP

  1. Jon E Steiner on February 27, 2019 at 4:06 pm said:

    I tried running this on Windows 10 IoT build 17763.316/Raspberry Pi 3 and it didn’t work. Any thoughts?

    • bsonnino on February 27, 2019 at 5:37 pm said:

      I didn’t try it on the Raspberry Pi, but the Windows 10 there is not a full one, I don’t think it’s available there, it’s just a desktop feature

Leave a Reply

Your email address will not be published. Required fields are marked *

Post Navigation