Merge DLL into EXE?

115,623

Solution 1

For .NET Framework 4.5

ILMerge.exe /target:winexe /targetplatform:"v4,C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0" /out:finish.exe insert1.exe insert2.dll

ILMerge

  1. Open CMD and cd to your directory. Let's say: cd C:\test
  2. Insert the above code.
  3. /out:finish.exe replace finish.exe with any filename you want.
  4. Behind the /out:finish.exe you have to give the files you want to be combined.

Solution 2

Use Costura.Fody.

You just have to install the nuget and then do a build. The final executable will be standalone.

Solution 3

Download ilmerge and ilmergre gui . makes joining the files so easy ive used these and works great

Solution 4

Reference the DLL´s to your Resources and and use the AssemblyResolve-Event to return the Resource-DLL.

public partial class App : Application
{
    public App()
    {
        AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
        {

            Assembly thisAssembly = Assembly.GetExecutingAssembly();

            //Get the Name of the AssemblyFile
            var name = args.Name.Substring(0, args.Name.IndexOf(',')) + ".dll";

            //Load form Embedded Resources - This Function is not called if the Assembly is in the Application Folder
            var resources = thisAssembly.GetManifestResourceNames().Where(s => s.EndsWith(name));
            if (resources.Count() > 0)
            {
                var resourceName = resources.First();
                using (Stream stream = thisAssembly.GetManifestResourceStream(resourceName))
                {
                    if (stream == null) return null;
                    var block = new byte[stream.Length];
                    stream.Read(block, 0, block.Length);
                    return Assembly.Load(block);
                }
            }
            return null;
        };
    }
}

Solution 5

Download

ILMerge

Call

ilmerge /target:winexe /out:c:\output.exe c:\input.exe C:\input.dll
Share:
115,623
Momro
Author by

Momro

Updated on July 05, 2022

Comments

  • Momro
    Momro almost 2 years

    I have two DLL files which I'd like to include in my EXE file to make it easier to distribute it. I've read a bit here and there how to do this, even found a good thread here, and here, but it's far too complicated for me and I need real basic instructions on how to do this.

    I'm using Microsoft Visual C# Express 2010, and please excuse my "low standard" question, but I feel like I'm one or two level below everyone else's expercise :-/ If someone could point out how to merge these DDL files into my EXE in a step-by-step guide, this would be really awesome!

  • Yuki Kutsuya
    Yuki Kutsuya about 12 years
    Add: /target:winexe /target platform:"v4,C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0" to it if you have a .NEt framework 4.5 application :).
  • Momro
    Momro about 12 years
    This results in the following error message: An exception occurred during merging: Unresolved assembly reference not allowed: System.Core. - I created a bat file with the following content: ilmerge /target:winexe my_exe.exe my_dll.dll /out:merged.exe
  • Momro
    Momro about 12 years
    Ok, I don't understand the slightest thing of what you wrote there ;-) Where do I have to put this code into?
  • Destructor
    Destructor about 12 years
    Are you using a WPF or a WindowsForms Application?
  • Destructor
    Destructor about 12 years
    Ok, first you have to add the DLL´s to your project-Resources. Add a folder "Resources" to your Solution and just put your dll´s in there. Right click on your project -> Properties -> Resources. There click "Add Resource" and add your dll´s. After that go to your program.cs file and add the code to your Main() before everything else.
  • Destructor
    Destructor about 12 years
    For WinForms try this code instead of the code above: AppDomain.CurrentDomain.AssemblyResolve += (sender, arg) => { if (arg.Name.StartsWith("Your_DLL")) return Assembly.Load(Properties.Resources.Your_DLL); return null; };
  • Yuki Kutsuya
    Yuki Kutsuya about 12 years
    @Momro Try with the target platform parameter ;).
  • Momro
    Momro about 12 years
    Okay, I used your shortened version, but my IDE tells me that the name "Assembly" is not available in this context :-/
  • Destructor
    Destructor about 12 years
    Add a using System.Reflection;
  • Destructor
    Destructor about 12 years
    And to ensure that the referenced dll´s are not copied to the output dir, you have to select your dll´s from References and set the property "Copy Local" to false.
  • Momro
    Momro about 12 years
    Ok, so I have this command: ilmerge /target:winexe /target:platform:"v4,c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\" /out:merged.exe my_exe.exe my_dll.dll, but the prompt says that I have to specify at least one input fil and an output file. I also exchanged the out parametre with the inputs with no result.
  • Momro
    Momro about 12 years
    So I included this code snippet for each DLL, but the EXE won't start if the DLLs aren't in the same folder (copied the EXE to desktop to test it). - And I couldn't find this property. How do I get to the References? If I go to the program's resources, I can't edit their properties (except for Comment, Filename, FileType, Persistence and Type).
  • Momro
    Momro about 12 years
    Hurray, that worked. The problem was a "\" at the end of the directory path which was too much.
  • Destructor
    Destructor about 12 years
    In your Solution Explorer there is a point "References". The Point, where you added your dll to use it in your program. There are all referenced dll listed. Select your dll and change "Copy Local" to false.
  • Momro
    Momro about 12 years
    I did that, even changing WMPLib's and AXVLC's Interop type to "false" to let me change "local copy" to false. I created a pastebin to show you the code snippet in my Program.cs: pastebin entry
  • AB Bolim
    AB Bolim over 10 years
    I just followed your instructions as you mention above. I have also put ilmerge to my system path. still not merged. please help me. In Command Prompt : >ilmerge myExe.exe mfc100u.dll mfc110u.dll /out:final.exe Error: An exception occurred during merging: ILMerge.Merge: Could not load assembly from the location 'C:\test\my folder\mfc 100u.dll'. Skipping and processing rest of arguments. Any idea whats gonin wrong ?
  • AB Bolim
    AB Bolim over 10 years
    I just followed your instructions as you mention above. still not merged. please help me. In Command Prompt : ILMerge.exe /target:winexe /targetplatform:"v4,C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0" /out:finish.exe myExe.exe mfc100u.dll mfc110u.dll msvcp110.dll Error: An exception occurred during merging: ILMerge.Merge: Could not load assembly from the location 'C:\test\my folder\mfc 100u.dll'. Skipping and processing rest of arguments. Any idea whats gonin wrong ?
  • Henrik Karlsson
    Henrik Karlsson over 10 years
    Looks like your dlls name contains a space? Try putting quotes around it
  • AB Bolim
    AB Bolim over 10 years
    Thanx for reply. But only in this my above comment, mfc100u.dll have space. I want to merge more than one dll file. I have also try ILMerge GUI. But still facing the same problem. Please help me.
  • rerat
    rerat over 8 years
    Could not get the above examples from other users to work, but this work perfectly. Thanks.
  • Brett Caswell
    Brett Caswell over 7 years
    this is a very interesting project. but it seems to be a plugin for Fody.. so you could improve this answer a bit by providing some information on Mono.Cecil and Fody (as they are both dependencies, and are really the most relevant to the underline premise). But, yea, the plugin itself does seem to make it 'easier' to embed and distribute dlls in a configurable manner.
  • Cristian Diaconescu
    Cristian Diaconescu over 6 years
    This is voodoo. Unbelievably easy. Install-Package Costura.Fody and Bam! your build will produce one big fat exe.
  • SWdV
    SWdV over 6 years
    Sometimes you have to 1) use Program Files (x86) 2) type the full path to ILMerge. Use /target:exe for console applications.
  • Antonios Tsimourtos
    Antonios Tsimourtos about 6 years
    Doesnt quite work but i support this is can happen in some cases. (Can't build and i have an error)
  • it3xl
    it3xl about 4 years
    For me it is the best way to update ILMerge, thanks @benscabbia
  • Daniel
    Daniel almost 4 years
    Nice, this works but you just need to specify RuntimeIdentifier or RuntimeIdentifiers and the file gains about 65.4 MB.
  • cmartin
    cmartin over 3 years
    Wow - I struggled for 3 days trying to get iLMerge to work with several 3rd party dlls that had multiple other references I had to point to, and even though I had all dependencies added it still did not work and upon examining the logs it was mutating some of the namespace names, saying they were duplicate. (They were inside the 3rd party assemblies) This worked just as easy as @CristianDiaconescu said.
  • Gray Programmerz
    Gray Programmerz over 2 years
    Windows by-default comes with .NET framework here C:\Windows\Microsoft.NET\Framework. So you can also use /targetplatform:"v2,C:\Windows\Microsoft.NET\Framework\v2.0.‌​50727" or /targetplatform:"v4,C:\Windows\Microsoft.NET\Framework\v4.0.‌​30319"