Collect a Visual Studio project into a single file using ILMerge

    app.exe, d1.dll d2.dll = app.exeUsually the result of compiling a project is the assembly of the project, as well as its dependency (Referenced Assemblies). However, sometimes you want the result to be a single file, a single file, independent of other assemblies. For example, a simple utility that can be copied anywhere and it will work.


    Relatively speaking after:
        compile App \ App.csproj  
        dir App \ bin \ Release  
    It turns out:
    We only need one self-sufficient
    That is, containing in itself dep1.dlland dep2.dll

    on Habré there is already a solution with embedding dependencies in resources, here I will show how to do this using ILMerge and Post Build Event in Visual Studio.

    Source code

    ILMerge - A program from Microsoft Research, which actually has the required functionality.
    merge_all.bat - we will use it in the Post-build event.


    Download and install ILMerge .
    Put %PROGRAMFILES%\ILMerge\ILMerge.exein the folder ${SolutionDir}ILMerge\
    In the same place, create the file merge_all.bat
    Add a line to [Project-> Properties-> Build Events-> Post-build event]:
    "$(SolutionDir)\ILMerge\merge_all.bat" "$(SolutionDir)" "$(TargetPath)" $(ConfigurationName)

    Contents merge_all.bat

    All assemblies from $ output will merge into one. If Debug configuration is in $ output \ Output, if Release then as a result there will be only one file in $ output. Information about how the merger went and what problems were written in Visual Studio Output. Comments explain what happens inside. For example, in this case, the .NET 4 platform is selected.

    rem #    set .NET version and output folder name
    set net="v4, C:\Windows\Microsoft.NET\Framework\v4.0.30319"
    set output=Output
    rem #    process arguments
    set ILMergeSolution=%1ILMerge\ILMerge.exe
    rem # determine programm files of x86 for 32 and 64 Platform
    IF     EXIST "%PROGRAMFILES(x86)%" set prorgammFiles=%PROGRAMFILES(x86)%
    IF NOT EXIST "%PROGRAMFILES(x86)%" set prorgammFiles=%PROGRAMFILES%
    rem #	if ILMerge.exe not in the $(SolutionDir)ILMerge\
    rem #		then try to use installed in prorgammFiles
    IF     EXIST %ILMergeSolution% set ILMerge="%ILMergeSolution%"
    IF NOT EXIST %ILMergeSolution% set ILMerge=%prorgammFiles%\Microsoft\ILMerge\ILMerge.exe
    set target_path=%2
    set target_file=%~nx2
    set target_dir=%~dp2
    set ConfigurationName=%3
    rem #    set output path and result file path
    set outdir=%target_dir%%output%
    set result=%outdir%\%target_file%
    rem #    print info
    @echo     Start %ConfigurationName% Merging %target_file%. 
    @echo Target: %target_path%
    @echo target_dir: %target_dir%
    @echo Config: %ConfigurationName% 
    rem #    recreate outdir
    IF EXIST "%outdir%" rmdir /S /Q "%outdir%"
    md "%outdir%"
    rem #    run merge cmd
    @echo Merging: '"%ILMerge%" /wildcards /targetplatform:%net% /out:"%result%" %target_path% "%target_dir%*.dll"'
    "%ILMerge%" /wildcards /targetplatform:%net% /out:"%result%" %target_path% "%target_dir%*.dll"
    rem #    if succeded
    IF %ErrorLevel% EQU 0 (
        rem #    clear real output folder and put there result assembly
        IF %ConfigurationName%==Release (
            del  %target_dir%*.*
            del  %target_dir%*.dll
            del  %target_dir%*.pdb
            del  %target_dir%*.xml
            del  %target_dir%*.*
            copy %result% %target_dir%
            rmdir /S /Q %outdir%
            set result=%target_path% 
            @echo Result: %target_file% "->  %target_path%"
        ) ELSE (
           @echo Result: %target_file% "->  %result%" )
       set status=succeded
       set errlvl=0    
    ) ELSE (
        set status=failed 
        set errlvl=1
    @echo Merge %status%
    exit %errlvl% 


    From ILMerge :
    Commercial use permitted:
    The language of ILMerge's license has raised many questions. In a nutshell: commercial use is permitted, redistribution is not. Read the license carefully for full details since I am not (nor do I wish to be!) A lawyer.

    That is, you can embed it in the assembly process, but you can’t distribute it with your product.

    The open question remained about the license of the merged dlls.

    • beliakov : ILMerge memory eats like a hippo ... On a large number of assemblies
    • braindamaged : ignores the key, which causes it, in turn, to ignore the assemblies and namespaces that are in the dependencies.

    Similar tools

    Related Materials

    Also popular now: