Embed DLLs in Single Executable File of Windows Forms App
Table of contents
Introduction
In this article, I will be talking about how you can deploy your Full-Stack Windows Forms Application into a single exe file.
Problem Statement
If you are working on a full-stack Windows Forms Application project and you want to have users for this application then how would you go about making this application accessible for all people to use as a single executable file?
My Own Project
When I published my Windows Forms .NET Framework application (Task Maker Application) which uses a hosted CockroachDB database, the .exe file worked in the project folder but when I would drag the .exe file to a different location it would show an error.
This is the error that would pop up: System.IO
.FileNotFoundException: Could not load file or assembly 'Npgsql, Version=7.0.6.0
It would look similar to this:
I had already installed the Npgsql Package in Nuget Package Manager but this error would still show.
Solution
The reason why I was getting this error message was because the Npgsql Nuget Package wasn't being embedded inside the .exe so the file couldn't locate the Npgsql Package and that's why the error kept happening. The Npgsql package was being used to connect the .NET application to my database.
In order to solve this problem I found a Nuget Package called Costura.Fody
. This package can embed all the necessary components being used in the program into a single executable file.
Install the package and then rebuild your project.
Once you have done that go to the directory containing your executable file and run it in a different location or computer. The executable can usually be found in the Debug directory inside the app.publish folder:
Congratulations you're full-stack app should work now 🥳
If you want to stay up to date with the development of this project then here is my repo: https://github.com/N00rAhmed/Task-Manager
Subscribe to my newsletter
Read articles from Noor Ahmed directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Noor Ahmed
Noor Ahmed
I am a software engineer intern from the UK and I have worked with multiple technologies like MERN stack, JavaScript, Python etc. I also like to contribute to open source on Github.