How to run .NET Core console application from the command line

.Net CoreCommand LineConsole Application

.Net Core Problem Overview


I have a .NET Core console application and have run dotnet publish. However, I can't figure out how to run the application from the command line. Any hints?

.Net Core Solutions


Solution 1 - .Net Core

If it's a framework-dependent application (the default), you run it by dotnet yourapp.dll.

If it's a self-contained application, you run it using yourapp.exe on Windows and ./yourapp on Unix.

For more information about the differences between the two app types, see the .NET Core Application Deployment article on .NET documentation.

Solution 2 - .Net Core

You can very easily create an EXE (for Windows) without using any cryptic build commands. You can do it right in Visual Studio.

  1. Right click the Console App Project and select Publish.
  2. A new page will open up (screen shot below)
  3. Hit Configure...
  4. Then change Deployment Mode to Self-contained or Framework dependent. .NET Core 3.0 introduces a Single file deployment which is a single executable.
  5. Use "framework dependent" if you know the target machine has a .NET Core runtime as it will produce fewer files to install.
  6. If you now view the bin folder in explorer, you will find the .exe file.
  7. You will have to deploy the exe along with any supporting config and dll files.

Console App Publish

Solution 3 - .Net Core

You can also run your application like any other console applications, but only after the publish.

Let's suppose you have the simple console application named MyTestConsoleApp.

Open the package manager console and run the following command:

dotnet publish -c Debug -r win10-x64 

The -c flag mean that you want to use the debug configuration (in other case you should use Release value)

The -r flag means that your application will be run on the Windows platform with an x64 architecture.

When the publish procedure will be finished you will see the *.exe file located in your bin/Debug/publish directory.

Now you can call it via command-line tools. So open the CMD window (or terminal) move to the directory where your *.exe file is located and write the next command:

>> MyTestConsoleApp.exe argument-list

For example:

>> MyTestConsoleApp.exe --input some_text -r true

Solution 4 - .Net Core

With .NET Core 3.0 you can package the entire solution into a single-file executable using the PublishSingleFile property:

-p:PublishSingleFile=True

Source: Single-file executables

An example of a self-contained, release OS X executable:

dotnet publish -c Release -r osx-x64 -p:PublishSingleFile=True --self-contained True

An example of a self-contained, debug Linux 64-bit executable:

dotnet publish -c Debug -r linux-x64 -p:PublishSingleFile=True --self-contained True

The Linux build is independent of distribution and I have found them working on Ubuntu 18.10 (Cosmic Cuttlefish), CentOS 7.7, and Amazon Linux 2.

A self-contained executable includes the .NET runtime and runtime does not require to be installed on a target machine. The published executables are saved under:

<ProjectDir>/bin/<Release or Debug>/netcoreapp3.0/<target-os>/publish/ on Linux, OS X and

<ProjectDir>\bin\<Release or Debug>\netcoreapp3.0\<target-os>\publish\ on Windows.

Solution 5 - .Net Core

Using CMD you can run a console .NET Core project if .NET Core SDK is installed on your machine:

To run a console project using the Windows command-Line, choose the specific path from your directory and type the following below command:

dotnet run

Solution 6 - .Net Core

Before you run in a command prompt, make sure "appsettings.json" has the same values as "appsettings.Development.json".

In a command prompt, go all the way to the bin/debug/netcoreapp2.0 folder. Then run "dotnet applicationname.dll"

Solution 7 - .Net Core

Go to ...\bin\Debug\net5.0 ("net5.0" can also be something like "netcoreapp2.2", depending on the framework you use.)

Enter image description here

Open a PowerShell window by clicking on it like shown in the picture.

Type in the PowerShell window: .\yourApp.exe

You don't need dotnet publish. Just make sure you build it before to include all changes.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondevlifeView Question on Stackoverflow
Solution 1 - .Net CoresvickView Answer on Stackoverflow
Solution 2 - .Net CoreJessView Answer on Stackoverflow
Solution 3 - .Net CoreTequilaView Answer on Stackoverflow
Solution 4 - .Net CoreTMTView Answer on Stackoverflow
Solution 5 - .Net CoreSaurabh NachankarView Answer on Stackoverflow
Solution 6 - .Net CorerahulView Answer on Stackoverflow
Solution 7 - .Net CoreLukeTheProgrammerView Answer on Stackoverflow