Publishing an application as a single file

For a while now we’re been able to turn our usual .exe and .dll’s into a single file, which ofcourse makes deployment very simple, let’s see what we need to change (in the .csproj of you EXE)

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <PublishSingleFile>true</PublishSingleFile>
    <SelfContained>true</SelfContained>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
  </PropertyGroup>
</Project>

The PublishSingleFile specifies if we should publish to a single EXE. The SelfContained when true means, include all the required .NET runtimes and can be run on any machine without requirement the .NET runtime to be installed. Finally the RuntimeIdentifier specifies the target platform and ensure the correct runtime files are included.

Note: We can specify the RuntimeIdentifier as part of the publish step if we prefer.

Options for this are

  • Windows
    • win-x86
    • win-x64
    • win-arm
    • win-arm64
  • Linux
    • linux-x64
    • linux-arm
    • linux-arm64
  • Mac OS
    • osx-x64
    • osx-arm64

Publishing

We would use the following command to publish our application

dotnet publish

More specifically we’d use commands such as

dotnet publish -r win-x64 -c Release
dotnet publish -r linux-x64 -c Release
dotnet publish -r osx-x64 -c Release

Where -r is the runtime (see the list above) and -c for the configuration.

Be aware that when you include the runtime you’re see an increase is the size of your self contained EXE, but now you just have the one file to release.