# 嵌入Dll 到.net 程序中的方法

### 我们经常会写一些小程序给自己或者他人用,而这些程序时长又会去引用一些第三方的Dll,比如开源的ICSharpCode.SharpZipLib.dll等,为了让程序保持整洁,或者给对方的时候方便,就想把这些dll给嵌入到EXE中去,这样在不打包的情况下,只要丢一个文件给对方就能用了.最近研究了下可行性,目前有如下两种方法:

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

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

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

   1: private static void ExtractResourceToFile(string resourceName, string filename)
   2:    {
   3:          if (!System.IO.File.Exists(filename))
   4:             using (System.IO.Stream s = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
   5:             usng (System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Create))
   6:             {
   7:                    byte[] b = new byte[s.Length];
   8:                    s.Read(b, 0, b.Length);
   9:                    fs.Write(b, 0, b.Length);
  10:              }
  11:     }

   1: static Program()
   2:       {
   3:           ExtractResourceToFile("EmbeddedDLL2ExeDemo.ThirdPartyDllDemo.dll",
   4:               "ThirdPartyDllDemo.dll");
   5:       }

这样就ok了.

2. ##### 方法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 @ 2010-06-28 22:34 塔夫男 阅读(...) 评论(...) 编辑 收藏