Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging dlls into a single .exe with wpf

Tags:

c#

wpf

dll

ilmerge

I'm currently working on a project where we have a lot of dependencies. I would like to compile all the referenced dll's into the .exe much like you would do with embedded resources. I have tried ILMerge but it can't handle .xaml resources.

So my question is: Is there a way to merge a WPF project with multiple dependencies into a single .exe?

like image 383
Farawin Avatar asked Jun 22 '09 07:06

Farawin


People also ask

How do I combine DLL files?

Yes, it is impossible to merge dll files; there is no tool to do it and it cannot be done manually either. You must modify the source code.

How add DLL to WPF?

Solution 1 Just look in the solution explorer and open your project. Right click "References" and select "Add Reference". Follow the dialogs to select the DLL file, add it and close the dialog. Your controls should then appear in the toolbox.


1 Answers

http://www.digitallycreated.net/Blog/61/combining-multiple-assemblies-into-a-single-exe-for-a-wpf-application

This worked like a charm for me :) and its completely free.

Adding code in case the blog ever disappears.

1) Add this to your .csproj file:

<Target Name="AfterResolveReferences">   <ItemGroup>     <EmbeddedResource Include="@(ReferenceCopyLocalPaths)" Condition="'%(ReferenceCopyLocalPaths.Extension)' == '.dll'">       <LogicalName>%(ReferenceCopyLocalPaths.DestinationSubDirectory)%(ReferenceCopyLocalPaths.Filename)%(ReferenceCopyLocalPaths.Extension)</LogicalName>     </EmbeddedResource>   </ItemGroup> </Target> 

2) Make your Main Program.cs look like this:

[STAThreadAttribute] public static void Main() {     AppDomain.CurrentDomain.AssemblyResolve += OnResolveAssembly;     App.Main(); } 

3) Add the OnResolveAssembly method:

private static Assembly OnResolveAssembly(object sender, ResolveEventArgs args) {     Assembly executingAssembly = Assembly.GetExecutingAssembly();     AssemblyName assemblyName = new AssemblyName(args.Name);      var path = assemblyName.Name + ".dll";     if (assemblyName.CultureInfo.Equals(CultureInfo.InvariantCulture) == false) path = String.Format(@"{0}\{1}", assemblyName.CultureInfo, path);      using (Stream stream = executingAssembly.GetManifestResourceStream(path))     {         if (stream == null) return null;          var assemblyRawBytes = new byte[stream.Length];         stream.Read(assemblyRawBytes, 0, assemblyRawBytes.Length);         return Assembly.Load(assemblyRawBytes);     } } 
like image 90
Wegged Avatar answered Oct 06 '22 13:10

Wegged