Smallest Single .NET Core Executable

Friday, 27 May 2022 - 1 minute to read

Use these settings in the main PropertyGroup in the csproj file to make the smallest, single .exe file possible.

  • OutputType = Exe
  • TargetFramework = net6.0 (or net6.0-windows)
  • PublishSingleFile = true
  • RuntimeIdentifier = win-x64
  • SelfContained = false

This corresponds to:

dotnet build project.csproj --use-current-runtime --no-self-contained -p:PublishSingleFile=true -o publish


dotnet build project.csproj -r win-x64 --no-self-contained -p:PublishSingleFile=true -o publish

PublishSingleFile will instruct the publisher to combine everything into one single .exe file. RuntimeIdentifier is necessary because it will become a platform-dependent file now. SelfContained defaults to true when we set RuntimeIdentifier, but we actually want SelfContained to be false, because it's okay if we have to install the .NET Core runtime. This strips a lot of megabytes from the resulting file.

Building and publishing is also fast.

Add a comment

  to leave a comment. It's quick and painless.