Merge DLL into EXE?
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
- Open CMD and cd to your directory. Let's say:
cd C:\test
- Insert the above code.
-
/out:finish.exe
replacefinish.exe
with any filename you want. - 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
Call
ilmerge /target:winexe /out:c:\output.exe c:\input.exe C:\input.dll
Momro
Updated on July 05, 2022Comments
-
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 about 12 yearsAdd:
/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 about 12 yearsThis 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 about 12 yearsOk, I don't understand the slightest thing of what you wrote there ;-) Where do I have to put this code into?
-
Destructor about 12 yearsAre you using a WPF or a WindowsForms Application?
-
Destructor about 12 yearsOk, 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 about 12 yearsFor 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 about 12 years@Momro Try with the target platform parameter ;).
-
Momro about 12 yearsOkay, I used your shortened version, but my IDE tells me that the name "Assembly" is not available in this context :-/
-
Destructor about 12 yearsAdd a using System.Reflection;
-
Destructor about 12 yearsAnd 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 about 12 yearsOk, 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 about 12 yearsSo 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 about 12 yearsHurray, that worked. The problem was a "\" at the end of the directory path which was too much.
-
Destructor about 12 yearsIn 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 about 12 yearsI 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 over 10 yearsI 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 over 10 yearsI 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 over 10 yearsLooks like your dlls name contains a space? Try putting quotes around it
-
AB Bolim over 10 yearsThanx for reply. But only in this my above comment,
mfc100u.dll
have space. I want to merge more than onedll
file. I have also tryILMerge GUI
. But still facing the same problem. Please help me. -
rerat over 8 yearsCould not get the above examples from other users to work, but this work perfectly. Thanks.
-
Brett Caswell over 7 yearsthis 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 over 6 yearsThis is voodoo. Unbelievably easy.
Install-Package Costura.Fody
and Bam! your build will produce one big fat exe. -
SWdV over 6 yearsSometimes you have to 1) use
Program Files (x86)
2) type the full path to ILMerge. Use/target:exe
for console applications. -
Antonios Tsimourtos about 6 yearsDoesnt quite work but i support this is can happen in some cases. (Can't build and i have an error)
-
it3xl about 4 yearsFor me it is the best way to update ILMerge, thanks @benscabbia
-
Daniel almost 4 yearsNice, this works but you just need to specify
RuntimeIdentifier
orRuntimeIdentifiers
and the file gains about 65.4 MB. -
cmartin over 3 yearsWow - 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 over 2 yearsWindows 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"