首先在资源管理里面将需要使用的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

最新文章

  1. tony_iptables_01_linux下IPTABLES配置详解(转)
  2. GeoHash
  3. C/C++ 笔试、面试题目大汇总(二)
  4. 将 node.js 的数据保存到 mongo 数据库中
  5. Codeforces Round #250 (Div. 2)——The Child and Set
  6. Delphi RichEx 图像
  7. 自定义实现InputFormat、OutputFormat、输出到多个文件目录中去、hadoop1.x api写单词计数的例子、运行时接收命令行参数,代码例子
  8. 通过代码创建label 计算最佳尺寸 让其自适应文本高度或宽度
  9. nginx upload module的使用
  10. hdu 1063 Exponentiation 大数
  11. [WebGL入门]十,矩阵计算和外部库
  12. Chapter 21_3 模式
  13. 递归求和1到n
  14. oracle导入时提示IMP-00010:不是有效的导出文件,头部验证失败
  15. Collections.synchronizedMap()、ConcurrentHashMap、Hashtable之间的区别
  16. Zookeeper初始(一)
  17. C#实现基于ffmpeg加虹软的人脸识别demo及开发分享
  18. git-github-TortoiseGit综合使用教程(一)简介
  19. web前端面试题HTML/CSS部分
  20. AES 加密256位 错误 java.security.InvalidKeyException: Illegal key size or default parameters

热门文章

  1. OPA-CH32x系列芯片OPA使用注意事项
  2. ResponseBodyAdvice处理返回数据
  3. GitLab CI-CD 学习笔记
  4. Spring Boot整合Redis-CRUD
  5. 【译】.NET 7 中的性能改进(二)
  6. Lucky Tree
  7. Android:LitePal 在第一次创建表之后第二次创建新的表不生效
  8. day08-MyBatis的关联映射02
  9. centos7中关闭防火墙命令
  10. cximage菜单(Load Jpeg Resource)