将.Net Core构建为EXE而不是DLL

我想将.NET Core项目构建为EXE而不是DLL,以便可以执行。

这里的答案不起作用: 如何运行.Net Core dll?

以下是示例代码:

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

这是我的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" } } } 

我目前正在使用VSCode,每当我使用构建任务构建项目,或运行dotnet restore我只在我的bin/Debug文件夹中获得.dll

如何将.NET核心应用程序构建为exe?

额外奖励:我这样做,会在Mac或其他设备上运行吗?

要生成EXE而不是DLL,您需要一个自包含的部署 。 您目前正在做的是依赖于框架的部署。 要将您的转换为自包含,请在project.json文件中执行以下步骤。

  1. 删除"type": "platform"
  2. 为您的应用支持的操作系统添加"runtimes"部分。

构建时,传入目标操作系统。 例如dotnet build -r osx.10.10-x64

这是结果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": {} } } 

另请参阅: https : //docs.microsoft.com/en-us/dotnet/articles/core/deploying/#self-contained-deployments-scd