程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> 嵌入Dll到.net程序中的方法

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

編輯:關於.NET

我們經常會寫一些小程序給自己或者他人用,而這些程序時長又會去引用一些第三方的Dll,比如開源的 ICSharpCode.SharpZipLib.dll等,為了讓程序保持整潔,或者給對方的時候方便,就想把這些dll給嵌入到EXE中去,這樣在不打包的情況下,只要丟一個文件給對方就能用了.最近研究了下可行性,目前有如下兩種方法:

方法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:是用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整合到一個可執行文件中。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved