Merging a WPF application into a single EXE

I always dislike handing off little applications to people. Not because I can’t, but because of the steps involved to make sure it all just works. Small apps are the most problematic because I never want to take the time to create a whole installer project for just a few assemblies, and packaging up a zip file must be accompanied by “Unzip this into a folder in your programs directory and create a shortcut…” which brings us back to the whole installer business we started with.

There are a few tools already out there such as ILMerge by Microsoft Research (Which works great for most .NET-y things, but chokes on WPF applications) and a few paid tools by third party vendors that you could fork over a few hundred for to get. But, I’m a developer, which means I want to do it the Hard Way™. I did a little research and found the following blog posts on setting up and merging in DLL’s as resources into the main assembly and then extracting and loading them into memory when you run your application.


There were a few things I didn’t like about each solution. The first one ( ends up having you directly adding the .dll’s as resources directly. I hate maintaining things manually, especially when it will run fine on my machine but break when when I move it somewhere else because I forgot to update the resources when I added a new project. The one from builds on the previous one and changes the resolve location to a custom class with its own startup method. Better, because it resolves the resources earlier. Finally, the one from Daniel Chambers ( added in the final piece that automatically including the assemblies as resources. Unfortunately, the way he looks for culture specific assemblies didn’t work and I had to remove / change it to be closer to the one on

Final solution I’m currently using is as follows:

To the main executable project, unload and edit the .csproj file, and below the following line:

  1. <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

Add this XML to the project file, save, and load it back up.

  1. <Target Name="AfterResolveReferences">
  2. <ItemGroup>
  3. <EmbeddedResource Include="@(ReferenceCopyLocalPaths)" Condition="'%(ReferenceCopyLocalPaths.Extension)' == '.dll'">
  4. <LogicalName>%(ReferenceCopyLocalPaths.DestinationSubDirectory)%(ReferenceCopyLocalPaths.Filename)%(ReferenceCopyLocalPaths.Extension)</LogicalName>
  5. </EmbeddedResource>
  6. </ItemGroup>
  7. </Target>

It should look something like this when your done:

You’ll then add a new code file to the main project and add the following code to it (modified to fit how your application is named / structured, in a WPF application, a good place to put it would be App.xaml.cs):

  1. [STAThread]
  2. public static void Main()
  3. {
  4. AppDomain.CurrentDomain.AssemblyResolve += OnResolveAssembly;
  6. App.Main(); // Run WPF startup code.
  7. }
  9. private static Assembly OnResolveAssembly(object sender, ResolveEventArgs e)
  10. {
  11. var thisAssembly = Assembly.GetExecutingAssembly();
  13. // Get the Name of the AssemblyFile
  14. var assemblyName = new AssemblyName(e.Name);
  15. var dllName = assemblyName.Name + ".dll";
  17. // Load from Embedded Resources - This function is not called if the Assembly is already
  18. // in the same folder as the app.
  19. var resources = thisAssembly.GetManifestResourceNames().Where(s => s.EndsWith(dllName));
  20. if (resources.Any())
  21. {
  23. // 99% of cases will only have one matching item, but if you don't,
  24. // you will have to change the logic to handle those cases.
  25. var resourceName = resources.First();
  26. using (var stream = thisAssembly.GetManifestResourceStream(resourceName))
  27. {
  28. if (stream == null) return null;
  29. var block = new byte[stream.Length];
  31. // Safely try to load the assembly.
  32. try
  33. {
  34. stream.Read(block, 0, block.Length);
  35. return Assembly.Load(block);
  36. }
  37. catch (IOException)
  38. {
  39. return null;
  40. }
  41. catch(BadImageFormatException)
  42. {
  43. return null;
  44. }
  45. }
  46. }
  48. // in the case the resource doesn't exist, return null.
  49. return null;
  50. }

Finally, make sure you update the target method for your main application to be the main method for the project you just added:

And, that’s it!

When you build your application you’ll still see all the assemblies in the output directory, but you should be able to take just the executable, move it somewhere else, and run it just as it is.

18 thoughts on “Merging a WPF application into a single EXE

  1. Hi Paul, thanks for the interesting idea!

    You’ve also written there are many commercial solution for this, I just note that there are freeware solution also, for eg, Enigma Virtual Box.

  2. Hi Paul, thanks for a simple solution that works for me.
    What’s the license of the code associated with this blog post?

    Is it ok to take your code and optimize it for my needs in commercial projects?

  3. Hey Paul, your solution is exact and very well explained it solved my problem. Thanks a lot..for posting.

    Just one request, Please provide some more instructions for the below code mentioned in your post like you did
    for the other [CSharp Target] one because for me it was hard to find where it should be placed and after some research I put in
    App.xaml.cs -> Application_Startup event and it worked perfectly

    public static void Main()
        AppDomain.CurrentDomain.AssemblyResolve += OnResolveAssembly;
        App.Main(); // Run WPF startup code.
  4. Thanks for the article, it worked great! One small thing I would add is that the code you posted should go in a class called “Program” nested inside of the App class to match the format shown in the screenshot where the main method is changed.

  5. Thanks for this solution. However, when trying to apply it to my program (putting your code in a new class called Program), I receive the following error message:

    has more than one entry point defined: ‘.Program.Main()’. Compile with /main to specify the type that contains the entry point.

    The application settings don’t show .Program as startup object :(

  6. … sorry – some characters got deleted when posting this comment. So, here it is again:

    Thanks for this solution. However, when trying to apply it to my program (putting your code in a new class called Program), I receive the following error message:

    [myapp] has more than one entry point defined: ‘[myapp].Program.Main()’. Compile with /main to specify the type that contains the entry point.

    The application settings don’t show [myapp].Program as startup object :(

  7. hi..
    i had used the above method but a particular dll is not getting merged into the solution WpfAnimatedGif.dll. Can you please help me in this.

  8. I have noticed you don’t monetize your page, don’t waste your traffic, you can earn extra bucks every month because you’ve got high
    quality content. If you want to know how to make extra money,
    search for: Ercannou’s essential adsense alternative

  9. This still works in 2018. :)

    But you better mention you need to create an extra “public class Program” to put all those code in the App.xaml.cs
    Putting the code under “public partial class App : Application” is incorrect

Leave a Reply

Your email address will not be published. Required fields are marked *