单个 .exe 文件太大

发布于 2025-01-16 12:08:17 字数 575 浏览 3 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水溶 2025-01-23 12:08:17

首先,如果可以的话,尝试升级到 .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

当梦初醒 2025-01-23 12:08:17

此命令发布适用于 Windows 64 位的 .NET 应用程序,创建单个可执行文件,而不在应用程序中包含 .NET 运行时。这有助于减少分发的可执行文件的大小。

dotnet publish -r win-x64 -p:PublishSingleFile=true --self-contained false

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.

dotnet publish -r win-x64 -p:PublishSingleFile=true --self-contained false
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文