生成.NET Core控制台应用程序以输出EXE

生成.NET Core控制台应用程序以输出EXE

问题描述:

对于面向.NET Core 1.0的控制台应用程序项目,我无法弄清楚如何在构建过程中输出.exe。该项目在调试中运行良好。

For a console application project targeting .NET Core 1.0, I cannot figure out how to get an .exe to output during build. The project runs fine in debug.

我尝试发布该项目,但这也不起作用。这是有道理的,因为EXE文件是特定于平台的,但是必须有一种方法。我的搜索只发现了使用project.json的.NET Core早期版本。

I've tried publishing the project, but that does not work either. It makes sense since an EXE file would be platform-specific, but there must be a way. My searches have only turned up reference to older .NET Core versions that used project.json.

无论何时构建或发布,这都是我得到的:

Whenever I build or publish, this is all I get:

出于调试目的,您可以可以使用DLL文件。您可以使用 dotnet ConsoleApp2.dll 运行它。如果要生成EXE文件,则必须生成一个自包含的应用程序。

For debugging purposes, you can use the DLL file. You can run it using dotnet ConsoleApp2.dll. If you want to generate an EXE file, you have to generate a self-contained application.

要生成自包含的应用程序(Windows中为EXE),必须指定目标运行时(特定于您目标的操作系统)。

To generate a self-contained application (EXE in Windows), you must specify the target runtime (which is specific to the operating system you target).

仅限Pre-.NET Core 2.0 :首先,添加运行时.csproj文件中的目标运行时的标识符(受支持的RID列表):

Pre-.NET Core 2.0 only: First, add the runtime identifier of the target runtimes in the .csproj file (list of supported RIDs):

<PropertyGroup>
    <RuntimeIdentifiers>win10-x64;ubuntu.16.10-x64</RuntimeIdentifiers>
</PropertyGroup>

从.NET Core 2.0开始,不再需要上述步骤。

然后,在发布应用程序时设置所需的运行时:

Then, set the desired runtime when you publish your application:

dotnet publish -c Release -r win10-x64
dotnet publish -c Release -r ubuntu.16.10-x64