立博名家

C#中用ILMerge将所有引用的DLL和exe文件打成一个exe文件

转载于:http://www.hv999.com/show.asp?id=1726

今天做了一个软件,想发布的时候才发现调用的类没几个,就像把它们都跟EXE文件打包在一起,以后复制去别的地方用也方便,于是上网搜了一下,发现网上大部分都是用ILMerge实现的,于是也自己试了一下,不过网上都没有详细的步骤演示,我就花点时间做了个教程,方便以后再有人想打包自己的程序, 有篇文章可以参考,好了废话少说,马上开始:

  1.先到http://www.microsoft.com/downloads/details.ASPx?FamilyID=22914587-B4AD-4EAE-87CF-B14AE6A939B0&displaylang=en下载ILMerge,才600多K,一下子就下好了.

  2.下载后是安装:

C#中用ILMerge将所有引用的DLL和exe文件打成一个exe文件

  3.不到一分钟就可以安装完毕:

  4.我是安装在I盘下ILMerge目录下,以下是安装后的文件,就一个ILMerge.exe文件而已,我们等会就是用它打包程序的.

  5.为了演示,我重新建了个工程,最后生成的是 Main.exe 和 newDll文件,其中newDll是Main.exe 中要引用的Dll文件,为了演示方便,我将它们都复制到了ILMerge的目录下,现在我们就试着用ILMerge将newDll和Main.exe文件打包起来吧.

  6.然后进入dos窗口,进入ILMerge目录,然后执行下面代码:

  I:\ILMerge\ILMerge.exe /ndebug /target:winexe  /out:newclient.exe MainExe.exe  /log newDll.dll

  7.ILMerge运行时的窗口,此时它正在努力的将Main.exe和newDll文件捆绑在一起:

  8.DOS窗口关闭后,我们在去ILMerge目录下看看,此时已经生成好了我们想要的newclient.exe文件,它就是Main.exe和newDll.dll的结合体啦:

 

方法1:把相关的第三方dll作为程序资源嵌入到EXE中,在程序运行的时候,从资源文件中输出到程序执行目录即可 


(图1:示例项目,ThirdPartydlldemo.dll作为第三方资源.Build Action属性设置为" Embedded Resource")

然后在Program.cs里面声明个静态构造函数,在该方法里面把第三方dll输出到程序目录,这样在调用第三方dll方法的时候,相关环境已经初始化完毕了.       

  private static void ExtractResourceToFile(string resourceName, string filename)
    {
       if (!System.IO.File.Exists(filename))
         using (System.IO.Stream s = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
         usng (System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Create))
         {
            byte[] b = new byte[s.Length];
            s.Read(b, 0, b.Length);
            fs.Write(b, 0, b.Length);
        }
    }复制代码

这样就ok了.


方法2:是用Ilmerge这个微软提供的工具,直接把相关的dll嵌入到目标exe中,而且程序运行时候,不像方法1会把相关的dll输出到可执行目录下,它直接让.net运行时到程序的资源中去找相关的dll引用,以下是Ilmerge的介绍: 

This document describes the ILMerge utility which merges multiple .NET assemblies into a single assembly. However, some .NET assemblies may not be able to be merged because they may contain features such as unmanaged code. I would highly recommend using peverify (the .NET Framework SDK tool) on the output of ILMerge to guarantee that the output is verifiable and will load in the .NET runtime.
ILMerge is packaged as a console application. But all of its functionality is also accessible programmatically. Note that Visual Studio does allow one to add an executable as a reference, so you can write a client that uses ILMerge as a library.
ILMerge takes a set of input assemblies and merges them into one target assembly. The first assembly in the list of input assemblies is the primary assembly. When the primary assembly is an executable, then the target assembly is created as an executable with the same entry point as the primary assembly. Also, if the primary assembly has a strong name, and a .snk file is provided, then the target assembly is re-signed with the specified key so that it also has a strong name.
Note that anything that depended upon any of the names of the input assemblies, e.g., configuration files, must be updated to refer instead to the name of the target assembly.
Any Win32 Resources in the primary assembly are copied over into the target assembly.
There are many options that control the behavior of ILMerge. These are described in the next section.
Ilmerge 相关的命令行参数是:
ilmerge [/lib:directory]* [/log[:filename]] [/keyfile:filename [/delaysign]] [/internalize[:filename]] [/t[arget] library|exe|winexe)] [/closed] [/ndebug] [/ver:version] [/copyattrs [/allowMultiple]] [/xmldocs] [/attr:filename] ([/targetplatform:<version>[,<platformdir>]]|v1|v1.1|v2|v4) [/useFullPublicKeyForReferences] [/zeroPeKind] [/wildcards] [/allowDup[:typename]]* [/allowDuplicateResources] [/union] [/align:n] /out:filename <primary assembly> [<other assemblies>...]
其中目标exe或者程序集,要放在输入的程序集里面的第一位置,其他dll放在它之后.其中/out:参数是必须的,其他参数可以参考文档
如图1 示例,命令行参数是 
ilmerge EmbeddedDLL2ExeDemo.exe ThirdPartyDllDemo.dll /ndebug /out:EmbeddedDll2Ex 
eDemo.exe
这样既可,该方法比方法1更完美,不过这个Ilmerge 在使用的时候还有一些不足的地方,比如/ver:version,这个参数设置后没有效果;

ilmerge就用自己把它引用到的两个dll嵌软到它自身里面了.

方法3:使用.Net混淆器都附带这样的功能,可以把多个dll整合到一个可执行文件中。

posted on 2012-02-06 09:36  大李  阅读(433)  评论(1)    收藏  举报

导航

立博名家