单个 .exe 文件太大
我正在尝试在 C# 中创建单个可执行文件。但是我不喜欢文件有多大。一个简单的 Hello World 程序本身就有 20mb。有没有一个好的方法来缩小它的大小,也许通过删除程序不使用的东西。
下面是我的 Hello World 测试程序的 .csproj 文件。我还想声明我正在发布模式下进行编辑,如果这有影响的话
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<PublishSingleFile>true</PublishSingleFile>
<PublishTrimmed>true</PublishTrimmed>
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
I am trying to create a single file executable in C#. However I don't like how large the file is. A simple Hello World program is 20mb on its own. Is there a good way to shrink the size of this, perhaps by removing things the program does not use.
Below is my .csproj file for the Hello World test program. I would also like to state I am editing in Release mode if this makes a difference
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<PublishSingleFile>true</PublishSingleFile>
<PublishTrimmed>true</PublishTrimmed>
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,如果可以的话,尝试升级到 .NET 6。修剪后的独立单文件 .NET 6 Hello World 控制台应用程序约为 11MB,而不是 20MB。
要更进一步,您可以尝试禁用框架功能:https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/trimming-options#trimming-framework-library-features
您可以压缩程序集,尽管这运行时会产生性能成本:https:// /learn.microsoft.com/en-us/dotnet/core/deploying/single-file/overview#compress-assemblies-in-single-file-app
如果您需要甚至此外,很快您就可以使用 NativeAOT 进行 AOT 编译;这将构建更小的可执行文件,因为它们不需要包含 JIT 编译器。 NativeAOT 将随 .NET 7 一起发布;请关注 .NET 7 预览版以获取说明。
您可能会发现有关该主题的这篇博客文章很有用:https://www. awise.us/2021/06/05/smallest-dotnet.html
First, try upgrading to .NET 6 if you can. A trimmed self-contained single-file .NET 6 Hello World console app is ~11MB, not 20MB.
To go further, you can try disabling framework features: https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/trimming-options#trimming-framework-library-features
You can compress assemblies, although this comes with a performance cost at runtime: https://learn.microsoft.com/en-us/dotnet/core/deploying/single-file/overview#compress-assemblies-in-single-file-app
And if you need to go even further, soon you'll be able to do AOT compilation using NativeAOT; this will build much smaller executables as they do not need to include the JIT compiler. NativeAOT will be shipping with .NET 7; keep an eye on the .NET 7 preview releases for instructions.
You may find this blog post on the topic useful: https://www.awise.us/2021/06/05/smallest-dotnet.html
此命令发布适用于 Windows 64 位的 .NET 应用程序,创建单个可执行文件,而不在应用程序中包含 .NET 运行时。这有助于减少分发的可执行文件的大小。
This command publishes a .NET application for Windows 64-bit, creating a single executable file without including the .NET runtime within the application. This helps reduce the size of the executable file for distribution.