[Home](../) › [Blog](../blog) › Pack your .NET program to a single .exe file with PostSharp Essentials

# Pack your .NET program to a single .exe file with PostSharp Essentials

> PostSharp released a free, open-source tool, PostSharp.Community.Packer, that enables users to pack their .NET Framework application into a single .exe file for easier distribution. The tool simplifies the process and makes it more convenient for end-users to download and run applications.

Published: 2020-05-27 · Author: Petr Hudeček · Canonical: https://postsharp.net/blog/pack-your-net-program-to-a-single-exe-file-with-postsharp-community

<p>Last month, we released <a
    href="https://github.com/postsharp/PostSharp.Community.Packer">PostSharp.Community.Packer</a>, a free and
  open-source tool that you can use to pack your .NET Framework application into a single .exe file for distribution.
</p>

<p>The basic idea as well as much of the code itself come from <a
    href="https://github.com/Fody/Costura">Costura.Fody</a>, an add-in to Fody, which has the same effect. We mostly
  differ only in the architecture we use to apply our changes to the build process. Like Costura, Packer is released
  under MIT.</p>
<p>You can find instructions on how to add Packer to your project on <a
    href="https://github.com/postsharp/PostSharp.Community.Packer">the Packer GitHub page</a>.</p>
<p>When I first encountered Costura.Fody, I was amazed. Before I discovered it, I needed to use an installer or complex
  tools like ILMerge/ILRepack, but Costura.Fody proved that you can have a self-contained .exe by adding a NuGet
  package, and it just works. So now we have that, too.</p>
<h2>Why should I distribute my application as a single .exe file?</h2>
<p>When you distribute an application, you want to make it as easy as possible for the end-user to download and start
  your application. The easier this process is, the more likely the user is to run your application.</p>
<p>Traditionally on Windows, you distribute applications using installers. But installers are heavyweight and represent
  a barrier themselves: users may not be as willing to install your application, especially if it&rsquo;s small, but
  they may be more willing to just run it.</p>
<p>Another common way to distribute an application is as a .zip file that the user needs to extract. But in this case,
  the user needs to have enough computer skills to know that they must extract rather than run directly from the .zip
  file (in which case your project would probably silently crash) and they must be able to identify the correct .exe
  file to run from possibly many files that are in the .zip file. This is a problem especially because the .dll files
  that you depend on are normally all in the same directory as your main .exe file.</p>
<p>Updating is also harder with a .zip file where the user must remember where they kept your application when they
  extract the update... and you&rsquo;d better hope that the possible extra files that are no longer required
  don&rsquo;t mess the application up.</p>
<p>Finally, there&rsquo;s the matter of aesthetics. A single file just looks so much cleaner than a folder with the main
  .exe file hiding among dozens of libraries!</p>
<p>Distributing your application as single .exe file allows the user to download only a small file and immediately run
  it. It doesn&rsquo;t clutter their computer as an installer would, and it doesn&rsquo;t have the risk that they
  won&rsquo;t be able to open it as a .zip file would.</p>
<h2>Can&rsquo;t .NET Core 3 already do this on its own?</h2>
<p><a href="https://docs.microsoft.com/en-us/dotnet/core/deploying/#publish-self-contained">That is true.</a> However,
  this would require you to use .NET Core.&nbsp;</p>
<p>Since .NET Framework is installed on every Windows machine, you can expect your small single-file .exe program to
  work everywhere. (If you want to be sure about older versions of Windows, you need to choose an older version of .NET
  Framework.) This means that your final .exe file can be even 1 MB or 2 MB in size. What is more, our telemetry data
  shows that, despite the hype .NET Core and .NET 5 are receiving, a majority of developers still target .NET
  Framework.&nbsp;</p>
<p>On the other hand, with .NET Core 3, you cannot usually expect the end-user to have .NET Core installed on their
  machine, and so you must include the runtime with your application, which means an extra 30&ndash;70 MB that the user
  needs to download and that can&rsquo;t be shared among applications.</p>
<h2>How does Packer work?</h2>
<p>Packer has two components: one works at build time and one at runtime.</p>
<p>At build time, Packer runs in the build process after the main compilation completes. It reads from the disk all
  assemblies that you have set as &ldquo;Copy to output folder&rdquo; which includes most referenced assemblies outside
  the .NET Framework itself. It also includes assemblies from NuGet packages. Then it puts the content of those
  assemblies as resources into your .exe file, optionally compressed.</p>
<p>At runtime, Packer registers itself as an assembly loader so that whenever the .NET Framework runtime fails to load a
  required assembly, it gives a chance to Packer to load it, and Packer will then uncompress the data in your
  application&rsquo;s resources and return that as the required assembly to the runtime.</p>
<h2>How can I add Packer to my project?</h2>
<p>It is very easy:</p>
<ol>
  <li>Install the NuGet package <a
      href="https://www.nuget.org/packages/PostSharp.Community.Packer/">PostSharp.Community.Packer</a>.</li>
  <li>Add <em>[assembly: Packer]</em> somewhere in your code.</li>
</ol>
<p>Then build your project and to test that everything is working, copy your resulting .exe file into a standalone
  folder and run it.</p>
<h2>Personal note</h2>
<p>At work, I often develop small tools to make life easier for other developers. I found that even there, among
  tech-savvy colleagues, distribution makes a big difference. Few colleagues would be willing to build a productivity
  tool from source, but many more would download and run a single .exe or .jar file (depending on the company&rsquo;s
  preferred language). Updates &ndash; manual or automatic &ndash; get easier as well.&nbsp;</p>
<p>Tools like PostSharp.Community.Packer truly extend your program&rsquo;s reach.</p>
<p>&nbsp;</p>

---

## Site navigation

- Index of the whole site: [llms.txt](../llms.txt)

