Creating a UWP Console app in C#

By | April 5, 2018

UWP Console apps

In my last two blogposts, I’ve already shown some really cool features coming in the Windows Redstone 4 update. Also new in the RS4 update is the ability to create UWP Console apps! As there currently is no project template available in Visual Studio, there are a couple of things that need to be set-up manually. If, in the future, the project template becomes available, this blogpost will still give you some background information about the ‘magic’ this template will do.

Update (07/06): Microsoft has released an update to the Console UWP App project templates on the Visual Studio marketplace. Find out more here.

Create new project and throw away everything…

… well, not everything 🙂
To get started, create a new UWP project in Visual Studio.

Then, select the appropriate UWP versions (at the moment of writing this is “Windows 10 Insider Preview Build 17120”). Once that is done, you can start removing some stuff! As we are building a Console app, we don’t need App.xaml(.cs) and MainPage.xaml(.cs), just delete these 4 files. Next, we need to add a new file: Program.cs. As a result, your project will look something like this:

Add DISABLE_XAML_GENERATED_MAIN Compilation Symbol

In the Properties window of our UWP app, we need to add DISABLE_XAML_GENERATED_MAIN to the Conditional Compilation Symbols. This prevents the build system from generating the default Main method.Conditional compilation symbolsBut how will our UWP start without a Main method? Simple, we are going to write our own!

Start building

In this newly created Program class, we are going to add a Main method:

Looks a lot like your classic Windows Console applications, doesn’t it? 🙂 Well, just like in your classic console applications, this is going to be our entry point. We now only need to hint Windows where it can find this Main method. For this, we need to open the Package.appxmanifest file.

To begin with, we need to add two extra namespaces: xmlns:uap5="http://schemas.microsoft.com/appx/manifest/uap/windows10/5"  and xmlns:desktop4="http://schemas.microsoft.com/appx/manifest/desktop/windows10/4"

Now, we can start using these namespaces in our appxmanifest. Next, we need to tell that this app will run in the console (and can run in multiple instances). This can be done by adding the following attributes to the Application element:  desktop4:Subsystem="console" desktop4:SupportsMultipleInstances="true" . Also we need point the EntryPoint attribute to the class, containing our Main method. So in the end, the entire Application tag will look something like this:

Now, we can already run the application from Visual Studio! If everything is correct, a Console window will pop-up. Probably, the window will also immediately close, just like with classic Console applications. So let’s put some code in our Main method:

Cool! Our first UWP Console app! And just like you would expect: if you add ‘Command Line Arguments’ to your Start options (right-click your project -> properties -> Debug), you’ll get them in your Main method through the args parameter!

But wait, there’s more!

Now we have an app that can be launched just like a regular UWP app, but instead of showing a UI, a console window opens up. That’s cool, but wouldn’t it be great if we could run our console app from, well…, the console itself?

In order to accomplish this, we need to add an AppExecutionAlias to the Appxmanifest file:

The Alias that you provide in the ExecutionAlias element, is the alias that you will be able to use from the console:

FYI: If you go to Settings in Windows and select Apps, there’s this link ‘Manage app execution aliases’. If you click this link, you’ll get an overview of all apps that have an alias registered on your system:

Let’s do something cool!

Now that we got our very basic app running, let’s talk to a UWP api from it. How about triggering a toast notification from the console?


You can find this code on GitHub.

Another example of a UWP Console app can be found in this blogpost. In this post we use a UWP Console app that does image classification using WinML.

That’s it

Yet another cool new UWP thing coming to RS4! Curious to see what tools or other stuff you guys will be building and publishing through the store! Or maybe some awesome text-based adventure game? (I’d pay for that 😉 )

8 thoughts on “Creating a UWP Console app in C#

  1. Pingback: Image classification with WinML and UWP – Pie Eating Ninjas' Blog

  2. Ezra

    Thanks so much for these instructions! I built a little UWP console app to update a file for myself, and it works fine as long as I build/run in the debug configuration. If I try to run it after building in the release configuration, the app wont launch. Any thoughts on why that might be the case?

    Reply
    1. PieEatingNinjas Post author

      Glad you like it!
      Concerning your problem, you might take a look at differences between your debug and release configuration. If you can’t find it out, please provide me your Solution so I could take a look at it, if you want.

      Reply
      1. Papa

        Ezra –
        Same thing here, but it works after unchecking “Compile with .NET Native tool chain” in project Properties/Build.

        PieEatingNinjas –
        I have a couple different questions to add: The console app comes up after the usual default Windows app window.. can that be skipped so only a console app runs?
        And there is a Windows Store app activation error after the console+window are closed.. can that be oppressed? [“Compile with .NET Native tool chain” being a store requirement, why bother..?]
        BTW, same behaviors with your GitHub project.

        Barring those minor issues this is undoubtedly a good testing tool for libraries, OAuth2 redirect (per custom app URIs, otherwise out-of-reach for normal console apps), etc.

        Reply
      2. Papa

        Hello – Initially I got this working despite being on too old of a version of Windows 10, however that produced an extra non-console window to appear and an “..unable to activate..” error. Those problems went away as soon as I updated to 1803.
        Since the issue Ezra saw (Release not working) only seems to be remedied by unchecking “Compile with .NET Native tool chain”, is it a forgone conclusion that console UWP apps cannot be sold @ the store?

        Reply
        1. Ezra

          Check out my comment above about there being a template from which to build the C# UWP console apps now. Fingers crossed it fixes this issue.

          Reply
  3. Mark Pflug

    Nice walk-through. You might clarify the “we need to add an AppExecutionAlias to the Appxmanifest file” with some context about where it needs to be added. I had to go look at the github project to figure that part out.

    Reply

Leave a Reply