Articles → .NET → Ilmerge Utility

Ilmerge Utility






What Is Ilmerge Utility?





Download




Picture showing the web page to download the ILMerge utility
Click to Enlarge



Picture showing the ILMerge folder once the ILMerge utility is installed
Click to Enlarge


Example




  1. A project of type ‘class library’. Let us name the project as ‘ILMergeLib’
  2. A console application that will use the DLL file. Let us name the project as ‘ILMergeDemo’


  1. Add following code in ‘ILMergeLib’ project
  2. namespace ILMergeLib {
    	public class MyClass {
    		public string SayHello(string name) {
    			return string.Format("Hello {0}", name);
    		}
    	}
    }




  3. 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)
  4. Add the reference of ‘ILMergeLib.dll’ in ‘ILMergeDemo’ project. See the screen shot below
  5. Picture showing adding the reference of ILMergeLib.dll in the project
    Click to Enlarge

  6. In ‘ILMergeDemo’ project, add the following code in ‘program.cs’.
  7. using System;
    using ILMergeLib;
    
    namespace ILMergeDemo {
    	class Program {
    		static void Main(string[] args) {
    			MyClass obj = new MyClass();
    			Console.WriteLine(obj.SayHello("gyan"));
    			Console.ReadLine();
    		}
    	}
    }


  8. Compile the ‘ILMergeDemo’ project so that executable file gets created in ‘bin\debug’ folder.

Running The Executable File






Picture showing the error message when executable file is run
Click to Enlarge




Merging Executable And DLL Files




  1. Copy the ILMerge.exe in the project folder. This step is optional.
  2. Right click on the project and click on ‘properties’.
  3. Picture showing the context menu when right-click on the project file in solution explorer
    Click to Enlarge

  4. In the properties window, click on ‘Build Events’ tab.
  5. In the ‘post-build event command line’ textbox, add the following command.
  6. "$(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"






  1. First parameter $(ProjectDir)ilmerge.exe specifies the path of ILMerge utility.
  2. Second parameter /target:exe specifies the type of output file when executable and DLL are merged. In this case, we want an exe file.
  3. Third parameter /out:$(TargetDir)ILMergeDemo_final.exe specifies the path and file name of the output file.
  4. 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.
  5. Last parameter /targetplatform:v4,C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0 specifies the .net framework.



Output




Picture showing the output of using the ILMerge utility
Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Thursday, August 3, 2017

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250