19

I want to build a .NET Core project as a EXE and not a DLL so it can be executed.

The answer here did not work: How to run a .Net Core dll?

Here is the sample code:

using System;

namespace ConsoleApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

Here is my project.json:

{
  "version": "1.0.0-*",
  "compilationOptions": {
    "emitEntryPoint": true
  },
  "buildOptions": {
    "debugType": "portable",
    "emitEntryPoint": true
  },
  "dependencies": {},
  "frameworks": {
    "netcoreapp1.1": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.1.0"
        }
      },
      "imports": "dnxcore50"
    }
  }
}

I'm currently using VSCode, whenever I build the project with the build task, or run dotnet restore I just get a .dll in my bin/Debug folder.

How do you build a .NET Core application as an exe?

Bonus: I do, will that run on a mac or other device?

Douglas Gaskell
  • 9,017
  • 9
  • 71
  • 128
  • Scott Hanselman wrote a [blog](http://www.hanselman.com/blog/SelfcontainedNETCoreApplications.aspx) that may answer this exact question – Mark C. Jan 17 '17 at 19:35
  • @MarkC. that worked great, though that has now broken the build and debugging through VSCode :/ – Douglas Gaskell Jan 17 '17 at 20:03
  • This question probably has the best answer at this point: https://stackoverflow.com/questions/51604179/net-core-2-1-dotnet-exe-on-build-packages-are-missing – Norman H Feb 05 '22 at 13:23

2 Answers2

8

I think most people got to this page because they picked .net core and they can't get an executable .exe file from their VS 2017/VS 2019 build. vs 2015 always used to make a .exe file for a console application. With vs 2017/vs 2019 when you create the project you get 2 choices for a Console application. One is Console App (.NET Core) and the other choice is Console App (.NET Framework). If you pick the .NET Core option you are going to have to move heaven and earth to get a .exe file from your Build. The (.NET Core) option creates a .dll file from a Build. If you pick the (.NET Framework) option it will build a xxxx.exe executable for you by default.

renaissanceMan
  • 385
  • 2
  • 7
  • 1
    Or you could specify `OutputType` on your csproj and set it to `Exe` – ColinM Aug 27 '20 at 21:27
  • 7
    Colin, appreciate the suggestion, agree that it would be a simple fix if it worked. With VS 2017 Enterprise I created a (.NET Core) Console App. the csproj file show Exe for Output type when I created it, didn't say "dll" as we might expect, and when I went to look in the bin/... directory there was only a .dll file and not a .exe file. – renaissanceMan Aug 28 '20 at 22:27
7

To produce an EXE instead of a DLL, you need a self-contained deployment. What you are currently doing is a framework-dependent deployment. To convert yours to self-contained, take the following steps in your project.json file.

  1. Remove "type": "platform".
  2. Add a "runtimes" section for the operating systems your app supports.

When you build, pass in the target operating system. E.g. dotnet build -r osx.10.10-x64.

This is the resultant project.json

{
  "version": "1.0.0-*",
  "compilationOptions": {
    "emitEntryPoint": true
  },
  "buildOptions": {
    "debugType": "portable",
    "emitEntryPoint": true
  },
  "dependencies": {},
  "frameworks": {
    "netcoreapp1.1": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "version": "1.1.0"
        }
      },
      "imports": "dnxcore50"
    }
  },
  "runtimes": {
    "win10-x64": {},
    "osx.10.10-x64": {}
  }
}

See also: https://learn.microsoft.com/en-us/dotnet/articles/core/deploying/#self-contained-deployments-scd

Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
  • 7
    As of March 2017, project.json has been deprecated in favor of a .csproj file. More info can be found here: https://learn.microsoft.com/en-us/dotnet/core/tools/project-json-to-csproj . According to this page, the runtimes element is now: `win7-x64;osx.10.11-x64;ubuntu.16.04-x64` . – Ada Richards Aug 01 '17 at 18:23