C# 将Dll文件打包到exe中

 

首先在资源管理里面将需要使用的dll添加进入

 

 

然后将dll文件的生成操作改成嵌入的资源

 

 

 

 

 

 

然后新建一个类  LoadResourceDll.cs

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Text.RegularExpressions;
 7 using System.Windows.Forms;
 8 using System.Diagnostics;
 9 using System.Reflection;
10  
11 namespace LegendTool
12 {
13     public static class LoadResoureDll
14     {
15         /// <summary> 已加载DLL
16         /// </summary>
17         private static Dictionary<string, Assembly> LoadedDlls = new Dictionary<string, Assembly>();
18         /// <summary> 已处理程序集
19         /// </summary>
20         private static Dictionary<string, object> Assemblies = new Dictionary<string, object>();
21         /// <summary> 在对程序集解释失败时触发
22         /// </summary>
23         /// <param name="sender">AppDomain</param>
24         /// <param name="args">事件参数</param>
25         private static Assembly AssemblyResolve(object sender, ResolveEventArgs args)
26         {
27             try
28             {
29                 //程序集
30                 Assembly ass;
31                 //获取加载失败的程序集的全名
32                 var assName = new AssemblyName(args.Name).FullName;
33                 //判断Dlls集合中是否有已加载的同名程序集
34                 if (LoadedDlls.TryGetValue(assName, out ass) && ass != null)
35                 {
36                     LoadedDlls[assName] = null;//如果有则置空并返回
37                     return ass;
38                 }
39                 else
40                 {
41                     throw new DllNotFoundException(assName);//否则抛出加载失败的异常
42                 }
43             }
44             catch (System.Exception ex)
45             {
46                 return null;
47                 MessageBox.Show("error:\n位置:AssemblyResolve()!\n描述:" + ex.Message);
48             }
49         }
50  
51         /// <summary> 注册资源中的dll
52         /// </summary>
53         /// <param name="pattern">*表示连续的未知字符,_表示单个未知字符,如*.dll</param>
54         public static void RegistDLL(string pattern = "*.dll")
55         {
56             System.IO.Directory.GetFiles("", "");
57             //获取调用者的程序集
58             var ass = new StackTrace(0).GetFrame(1).GetMethod().Module.Assembly;
59             //判断程序集是否已经处理
60             if (Assemblies.ContainsKey(ass.FullName))
61             {
62                 return;
63             }
64             //程序集加入已处理集合
65             Assemblies.Add(ass.FullName, null);
66             //绑定程序集加载失败事件(这里我测试了,就算重复绑也是没关系的)
67             AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolve;
68             //获取所有资源文件文件名
69             var res = ass.GetManifestResourceNames();
70             var regex = new Regex("^" + pattern.Replace(".", "\\.").Replace("*", ".*").Replace("_", ".") + "$", RegexOptions.IgnoreCase);
71             foreach (var r in res)
72             {
73                 //如果是dll,则加载
74                 if (regex.IsMatch(r))
75                 {
76                     try
77                     {
78                         var s = ass.GetManifestResourceStream(r);
79                         var bts = new byte[s.Length];
80                         s.Read(bts, 0, (int)s.Length);
81                         var da = Assembly.Load(bts);
82                         //判断是否已经加载
83                         if (LoadedDlls.ContainsKey(da.FullName))
84                         {
85                             continue;
86                         }
87                         LoadedDlls[da.FullName] = da;
88                     }
89                     catch (Exception ex)
90                     {
91                         MessageBox.Show("error:加载dll失败\n位置:RegistDLL()!\n描述:" + ex.Message);
92                     }
93                 }
94             }
95         }
96     }
97 }

在程序入库添加引用

LoadResoureDll.RegistDLL();

  

 

 

 

 参考原文:https://blog.csdn.net/yanhuatangtang/article/details/76228155

posted @ 2022-05-19 10:49  随遇而安(huangjun)  阅读(1745)  评论(2编辑  收藏  举报