.NET核心3.0
.NET Core 3.0 开箱即用地支持它。它将所有的东西打包在一起.exe
文件(基本控制台应用程序约为 68 MB)。有PublishTrimmed=true https://devblogs.microsoft.com/dotnet/announcing-net-core-3-0-preview-6/通过分析静态代码引用并从最终构建中排除未使用的框架程序集,可以将大小减小到约 28 MB。
配置single exe
构建编辑你的csproj
file:
<PropertyGroup>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<PublishSingleFile>true</PublishSingleFile>
</PropertyGroup>
or在文件夹中的命令行上csproj
file:
dotnet publish -r win-x64 -p:PublishSingleFile=true
有关更多详细信息,请参阅 Gopi 给出的精彩答案。
独立实用程序
Warp https://github.com/dgiagio/warp(感谢 Darien Shannon 在评论中提到)和dotnet CoreRT https://github.com/dotnet/corert。两者都适用于以前版本的 .Net Core
Warp https://github.com/dgiagio/warp
这是一个类似于为了经典.NET Framework
。这是非常容易使用。对于基本的控制台应用程序,它可以生成.exe
不使用摇树器时约为 35 MB,使用摇树器时约为 10-15 MB。
点网核心RT https://github.com/dotnet/corert
自 2022 年 1 月起,该项目被取代原生AOT https://github.com/dotnet/runtimelab/tree/feature/NativeAOT在 dotnet/runtimelab 存储库中进行实验。感谢@t.j。
目前,您可以尝试使用以下命令将应用程序预编译为本机单文件可执行文件dotnet CoreRT https://github.com/dotnet/corert项目。我说“尝试”是因为文档says https://github.com/dotnet/corert/tree/master/Documentation:
该项目正处于开发的早期阶段。
尽管如此,它至少适用于简单的应用程序。查看示例here https://github.com/dotnet/corert/tree/master/samples/HelloWorld。
根据其描述,需要在项目文件夹中运行以下命令:
dotnet new nuget
这会将 nuget.config 文件添加到您的应用程序中。打开文件
并在下面的元素中添加以下内容:
<add key="dotnet-core" value="https://dotnet.myget.org/F/dotnet-core/api/v3/index.json" />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
然后运行这个:
dotnet add package Microsoft.DotNet.ILCompiler -v 1.0.0-alpha-*
然后运行这个:
dotnet publish -r win-x64 -c release
完成后,您可以在根文件夹中找到本机可执行文件
您的项目位于 /bin/x64//netcoreapp2.0/publish/ 下