Articles → .NET → Ilmerge Utility
Ilmerge Utility
What Is Ilmerge Utility?
Download
Click to Enlarge
Click to Enlarge
Example
- A project of type ‘class library’. Let us name the project as ‘ILMergeLib’
- A console application that will use the DLL file. Let us name the project as ‘ILMergeDemo’
- Add following code in ‘ILMergeLib’ project
namespace ILMergeLib {
public class MyClass {
public string SayHello(string name) {
return string.Format("Hello {0}", name);
}
}
}
- Compile the code to make sure there is no error. Once the code is compiled, a file ‘ILMergeLib.dll’ will be created in bin/debug folder (if you are compiling your project in debug mode)
- Add the reference of ‘ILMergeLib.dll’ in ‘ILMergeDemo’ project. See the screen shot below
Click to Enlarge
- In ‘ILMergeDemo’ project, add the following code in ‘program.cs’.
using System;
using ILMergeLib;
namespace ILMergeDemo {
class Program {
static void Main(string[] args) {
MyClass obj = new MyClass();
Console.WriteLine(obj.SayHello("gyan"));
Console.ReadLine();
}
}
}
- Compile the ‘ILMergeDemo’ project so that executable file gets created in ‘bin\debug’ folder.
Running The Executable File
Click to Enlarge
Merging Executable And DLL Files
- Copy the ILMerge.exe in the project folder. This step is optional.
- Right click on the project and click on ‘properties’.
Click to Enlarge
- In the properties window, click on ‘Build Events’ tab.
- In the ‘post-build event command line’ textbox, add the following command.
"$(ProjectDir)ilmerge.exe" /target:exe /out:"$(TargetDir)ILMergeDemo_final.exe" "$(TargetDir)ILMergeDemo.exe" "$(TargetDir)ILMergeLib.dll" /targetplatform:"v4,C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0"
- First parameter $(ProjectDir)ilmerge.exe specifies the path of ILMerge utility.
- Second parameter /target:exe specifies the type of output file when executable and DLL are merged. In this case, we want an exe file.
- Third parameter /out:$(TargetDir)ILMergeDemo_final.exe specifies the path and file name of the output file.
- Fourth parameter $(TargetDir)ILMergeDemo.exe $(TargetDir)ILMergeLib.dll specifies the list of files that are merged into the single file. All the file names are separated by a space.
- Last parameter /targetplatform:v4,C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0 specifies the .net framework.
Output
Click to Enlarge