'Single .exe file is too large
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>
Solution 1:[1]
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://docs.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://docs.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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Reilly Wood |
