c#項目將dll打包到exe中的步驟
更新時間:2021年04月02日 11:58:07 作者:zls365
這篇文章主要介紹了c#項目將dll打包到exe中的步驟,幫助大家更好的理解和學習使用c#,感興趣的朋友可以了解下
意圖:
想將項目用到的兩個dll庫文件(CryptEnDe.dll和ICSharpCode.SharpZipLib.dll)一同編譯進exe中,并編譯后僅一個exe程序就可以獨立運行不再需要其它文件。
實現(xiàn):
1、將兩個dll庫文件作為資源文件添加進項目中;
2、添加功能代碼
using System; using System.Collections.Generic; using System.Diagnostics; using System.Reflection; using System.IO; namespace AutoUpdateServer.Core { /// <summary> 載入資源中的動態(tài)鏈接庫(dll)文件 /// </summary> static class LoadResourceDll { static Dictionary<string, Assembly> Dlls = new Dictionary<string, Assembly>(); static Dictionary<string, object> Assemblies = new Dictionary<string, object>(); static Assembly AssemblyResolve(object sender, ResolveEventArgs args) { //程序集 Assembly ass; //獲取加載失敗的程序集的全名 var assName = new AssemblyName(args.Name).FullName; //判斷Dlls集合中是否有已加載的同名程序集 if (Dlls.TryGetValue(assName, out ass) && ass != null) { Dlls[assName] = null;//如果有則置空并返回 return ass; } else { throw new DllNotFoundException(assName);//否則拋出加載失敗的異常 } } /// <summary> 注冊資源中的dll /// </summary> public static void RegistDLL() { //獲取調用者的程序集 var ass = new StackTrace(0).GetFrame(1).GetMethod().Module.Assembly; //判斷程序集是否已經處理 if (Assemblies.ContainsKey(ass.FullName)) { return; } //程序集加入已處理集合 Assemblies.Add(ass.FullName, null); //綁定程序集加載失敗事件(這里我測試了,就算重復綁也是沒關系的) AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolve; //獲取所有資源文件文件名 var res = ass.GetManifestResourceNames(); foreach (var r in res) { //如果是dll,則加載 if (r.EndsWith(".dll", StringComparison.OrdinalIgnoreCase)) { try { if (r.Contains("CryptEnDe.dll") || r.Contains("CryptEnDe_d.dll")) { ExtractResourceToFile(r, PathUtils.GetUpdateDllPath() + @"/" + r.Substring(r.IndexOf('.') + 1)); } var s = ass.GetManifestResourceStream(r); var bts = new byte[s.Length]; s.Read(bts, 0, (int)s.Length); var da = Assembly.Load(bts); //判斷是否已經加載 if (Dlls.ContainsKey(da.FullName)) { continue; } Dlls[da.FullName] = da; } catch(Exception e) { //加載失敗就算了... } } } } 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)) { using (System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite)) { byte[] b = new byte[s.Length]; s.Read(b, 0, b.Length); fs.Write(b, 0, b.Length); } } } } } }
其中PathUtils.GetUpdateDllPath()函數為獲取dll釋放的路徑,根據自己需要指定路徑
public static string GetUpdateDllPath() { string strPath = @"C:/LTShiyi/cache/updatedll"; if (!Directory.Exists(strPath)) { Directory.CreateDirectory(strPath); } return strPath; }
3、在程序入口Program類中調用上面的接口函數
static Program() { AutoUpdateServer.Core.LoadResourceDll.RegistDLL(); }
4、編譯即可。
以上就是c#項目將dll打包到exe中的步驟的詳細內容,更多關于c# dll打包到exe的資料請關注腳本之家其它相關文章!
相關文章
c#異步操作async?await狀態(tài)機的總結(推薦)
這篇文章主要介紹了c#異步操作async?await狀態(tài)機的總結,關于async和await每個人都有自己的理解,甚至關于異步和同步亦或者關于異步和多線程每個人也都有自己的理解,本文通過實例代碼詳細講解,需要的朋友可以參考下2023-02-02WPF實現(xiàn)Interaction框架的Behavior擴展
這篇文章介紹了WPF實現(xiàn)Interaction框架Behavior擴展的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06C#在WinForm中使用WebKit傳遞js對象實現(xiàn)與網頁交互的方法
這篇文章主要介紹了C#在WinForm中使用WebKit傳遞js對象實現(xiàn)與網頁交互的方法,涉及針對WebBroswer控件及WebKit控件的相關使用技巧,需要的朋友可以參考下2016-03-03C#的Socket實現(xiàn)UDP協(xié)議通信示例代碼
本篇文章主要介紹了C#的Socket實現(xiàn)UDP協(xié)議通信示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01