Bundle All Assemblies Into One Exe in C# with ILRepack
Recently, I built a little tool to automate an installation process that was pretty annoying.
I used C# and Windows.Forms.
In the middle, I found myself "needing" (because I like one-liners 😅) a library to work with JSON.
I put a reference to Newtonsoft.Json in my project.
Finished up and hit Build Solution.
And my release had the extra dll file for it.
I sent these files to my friend and tried to walk through it on discord (no screen sharing).
For whatever reason, the exe being distributed with multiple files was causing us issues.
It would have been easier if I could have bundled everything into that one exe.
So I figured out how to do that.
Configuring Nuget.org as a Package Source.
ILRepack is a newer replacement of Microsoft's deprecated ILMerge.
It will combine our assemblies with the exe.
It's not available from Visual Studios offline packages.
Right-click on your project in Visual Studio, and click "Manage Nuget Packages".
It's available from Nuget.org. You'll need to add that feed to your package sources.
In the top right side, next to "Package source: (dropdown)", hit the cog. A new Options box will pop up.
Click the green + button in the top right.
Add a name that makes sense to you (Nuget.org)
Add this source: https://api.nuget.org/v3/index.json
Press OK.
Install ILRepack
Select Nuget.org as your package source.
Click the Browse tab on the left and search ILRepack. Click on it and hit the install button
Build your exe
Pick your build type. In this tutorial, I'll build a Release. Just make sure you change the path if you are building a Debug or custom configuration exe.
From the Build menu, build your solution. That will place the built exe in your-project/bin/Release/your-project.exe.
Combining with your assemblies
When you installed ILRepack, it placed it in the directory with your sln file, in a 'packages' folder.
Click the Tools menu. Then command line > Powershell
Change into that directory. You'll have a different version than I do.
cd ./packages/ILRepack.2.x.x/tools
From here you can run this command to combine the dll files with your exe.
.\ILRepack.exe /out:C:\Users\nick\path\to\bin\ProgramMerged.exe C:\Users\nick\path\to\bin\Program.exe C\Users/nick\path\to\bin\third-party.dll
That's it! One file to distribute.