C# 嵌入dll 的方法
作者:树荫下的阳光 时间:2022-11-06 08:39:10
在很多时候我们在生成C#exe文件时,如果在工程里调用了dll文件时,那么如果不加以处理的话在生成的exe文件运行时需要连同这个dll一起转移,相比于一个单独干净的exe,这种形式总归让人不爽,那么有办法让生成的软件中直接就包含这个dll文件吗,这样就可以不用dll跟着exe走了,避免单独不能运行的情况。
答案是有的!
在工程项目目录下找到Resources.resx文件并点击,然后按下面操作,添加资源,将你要加入的dll添加进来。
操作完成后,就会在下面的内容框里看到你添加进来的dll。
然后在工程中加入下面这个函数代码:
System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
string dllName = args.Name.Contains(",") ? args.Name.Substring(0, args.Name.IndexOf(',')) : args.Name.Replace(".dll", "");
dllName = dllName.Replace(".", "_");
if (dllName.EndsWith("_resources")) return null;
System.Resources.ResourceManager rm = new System.Resources.ResourceManager(GetType().Namespace + ".Properties.Resources", System.Reflection.Assembly.GetExecutingAssembly());
byte[] bytes = (byte[])rm.GetObject(dllName);
return System.Reflection.Assembly.Load(bytes);
}
在InitializeComponent();之前调用。这样生成的exe就包含这个dll文件啦。
public Form1()
{
this.StartPosition = FormStartPosition.CenterScreen;
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
InitializeComponent();
}
总结
以上所述是小编给大家介绍的C# 嵌入dll 的方法,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
来源:https://www.cnblogs.com/wangshucai/archive/2018/07/07/9277822.html
标签:C#,嵌入,dll
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring Validation方法实现原理分析
2023-09-04 17:11:55
![](https://img.aspxhome.com/file/2023/7/81257_0s.jpg)
ProtoStuff不支持BigDecimal序列化及反序列化详解
2022-09-19 06:53:30
![](https://img.aspxhome.com/file/2023/1/110791_0s.png)
Spring Boot整合Lombok的方法详解
2023-11-22 09:06:21
![](https://img.aspxhome.com/file/2023/7/85497_0s.jpg)
Monkeyrunner 常用按键总结
2022-08-23 09:17:17
C# 获取指定QQ头像绘制圆形头像框GDI(Graphics)的方法
2023-03-26 14:44:41
![](https://img.aspxhome.com/file/2023/4/72484_0s.gif)
Android 自定义标题栏的实例详解
2021-11-06 00:53:04
Java深入理解代码块的使用细节
2023-06-13 04:45:59
![](https://img.aspxhome.com/file/2023/0/76610_0s.png)
JDK8中新增的原子性操作类LongAdder详解
2023-06-19 22:02:58
![](https://img.aspxhome.com/file/2023/3/76303_0s.jpg)
android从资源文件中读取文件流并显示的方法
2022-05-29 14:11:56
鉴权认证+aop+注解+过滤feign请求的实例
2022-06-05 14:25:34
![](https://img.aspxhome.com/file/2023/2/74882_0s.jpg)
JavaFX Metro UI 和 开发库使用简介
2021-12-11 19:01:39
![](https://img.aspxhome.com/file/2023/1/129191_0s.gif)
Java实现导入导出Excel文件的方法(poi,jxl)
2021-09-13 21:12:35
Android自定义GestureDetector实现手势ImageView
2023-12-18 05:16:35
java实现点击按钮弹出新窗体功能
2022-02-18 14:39:55
![](https://img.aspxhome.com/file/2023/4/119154_0s.jpg)
Java实战之用Swing实现通讯录管理系统
2021-08-15 08:24:09
![](https://img.aspxhome.com/file/2023/5/71905_0s.jpg)
详解android 用webview加载网页(https和http)
2021-12-29 11:14:30
Java进程cpu占用过高问题解决
2021-08-09 00:16:59
![](https://img.aspxhome.com/file/2023/7/70337_0s.jpg)
Java字符串操作和C#字符串操作的不同小结
2022-02-15 07:21:14
Java实现为Word每一页设置不同图片水印的效果
2023-01-30 07:10:04
![](https://img.aspxhome.com/file/2023/0/85830_0s.jpg)
android dialog自定义实例详解
2022-03-15 20:29:22
![](https://img.aspxhome.com/file/2023/7/138687_0s.jpg)