How to Create Single File Deployments in .Net 6 / VS2022How to dotnet build to create single file deployments where all dependencies are bundled into one executable, even the .Net runtime.

Single file deployments in Visual Studio allow developers to package their applications into a single executable file, including all necessary dependencies. This simplifies deployment and distribution, making sharing and running applications across different environments easier without worrying about missing dependencies or complex installation processes.
This guide will show two methods for building projects using single-file deployment. MSBuild will publish your project and compile a single executable file that contains all the required assemblies, including 3rd party dependencies and even the .Net framework runtime libraries. Although the resulting executable is larger than usual, it allows a single file to be deployed or distributed, and no other dependencies are required. It also provides portable applications, so you don't need to install extra libraries, third-party tools or frameworks on each machine it will run on.
If you have no external dependencies on your project, the first option works the best; however, if you have external dependencies such as DirectX, Win32 or any time you reference an external DLL, the second option will bundle these as well.
Bundle Everything to Single EXE with Single File Deployments
This option is the easiest as long as you don't have any external dependencies. To set up single file deployments, follow these steps.
- Right-click on the project to deploy and click Publish.
- Select Publish to Folder from the Target and Specific Target selections.
- Select a folder location to publish your project
- Once the profile is created, click the edit pencil next to Target Runtime.
- Set the target framework to net6.0, deployment mode to self-contained, target runtime to win-x64/win-x86 as appropriate.
- Click the File publish options and tick the Produce single file checkbox.
- Save, then publish

Bundle External Dependencies into Single EXE
Suppose you have any external dependencies or references to third-party DLL. In that case, you need to add additional configuration options which, for some reason, are not in the publish wizard. We can do this in one of two ways: publishing the project via the command line or editing the profile XML file.
Publishing for Single File Deployment on Command Line
Depending on whether you want to bundle the .Net runtime libraries, there are two different commands.
Use this command to publish WITH the .Net core runtime (will create a large file)
dotnet publish -p:PublishSingleFile=true -r win-x64 -c Release --self-contained true -p:IncludeNativeLibrariesForSelfExtract=true
To publish without .NET core runtime, when the application users already have the runtime installed, use this.
dotnet publish -p:PublishSingleFile=true -r win-x64 -c Release --self-contained false
Editing Publish Settings XML
It is often easier to edit the XML file to continue to build and publish within Visual Studio without dropping to the command line each time.
Open the project .csproj file in the editor (text mode) and locate the PropertyGroup section, usually near the top. Add in the two lines <PublishSingleFile>
and <IncludeAllContentForSelfExtract>
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
<PublishSingleFile>true</PublishSingleFile>
<IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>
</PropertyGroup>
Save the file, then publish the project as usual.
Enable Compression for Smaller Single File Deployment Bundles
Because the published executable now contains any dependant libraries and the .Net framework runtimes, the file size will likely be large, in the 150MB+ range. To combat this, we can enable compression to reduce the size. Enabling compression will create smaller executable files. However, this is at the cost of performance since the bundled assemblies will need to be decompressed into memory when launching the application. You should experiment with the option and see what works best for you.
To enable compression on single file deployments, edit the .csproj and add the following EnableCompressionInSingleFile
option to the XML.
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
<PublishSingleFile>true</PublishSingleFile>
<IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>
<EnableCompressionInSingleFile>true</EnableCompressionInSingleFile>
</PropertyGroup>