C#把dll分别放在指定的文件夹的方法步骤
作者:故里2130 时间:2022-08-19 10:03:51
C#客户端程序,生成后是一个exe,如果带有大量的dll,那么dll和exe会混乱在一起,看起来非常混乱,我们可以建立一个文件夹,把dll放进去,这样看起来就非常的清晰美观。
一共有二种方法
第一种,配置方法。
1.我们建立一个winform程序,对2个dll分别引用,调用里面的方法
生成后的文件是这样的
2.打开App.config文件夹,其中dll和dll/2相当于文件夹
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<!--<publisherPolicy apply="yes" />这句不要也是可以的-->
<probing privatePath="dll;dll/2" />
</assemblyBinding>
</runtime>
</configuration>
3.选择所有的dll,把复制本地设置成 FALSE
4.打开项目的exe路径,分别建立dll文件夹,把其中一个dll放进去
建立dll/2文件夹,把另一个dll放进去
5.文件夹的效果
WindowsFormsApp4.exe
WindowsFormsApp4WindowsFormsApp4.exe.config
dll
...../ClassLibrary1.dll
...../2/ClassLibrary2.dll
6.效果,这样就比较好看一些。
第二种,代码方法
1.同样建立一个项目,选择所有的dll,把复制本地设置成 FALSE
2.在窗体的初始化出写入
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
string path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"dll2\");
path = System.IO.Path.Combine(path, args.Name.Split(',')[0]);
path = String.Format(@"{0}.dll", path);
return System.Reflection.Assembly.LoadFrom(path);
}
3.在项目的debug文件夹中,建立代码中的名字dll2文件夹,把所有的dll扔进去即可。
4.代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
}
private void Form1_Load(object sender, EventArgs e)
{
ClassLibrary1.Class1 c = new ClassLibrary1.Class1();
ClassLibrary2.Class1 c1 = new ClassLibrary2.Class1();
MessageBox.Show(c.A() + c1.B());
}
/// <summary>
/// 对外解析dll失败时调用
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
/// <returns></returns>
static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
string path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"dll2\");
path = System.IO.Path.Combine(path, args.Name.Split(',')[0]);
path = String.Format(@"{0}.dll", path);
return System.Reflection.Assembly.LoadFrom(path);
}
}
}
来源:https://blog.csdn.net/u012563853/article/details/124803478
标签:C#,dll,文件夹
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
判断一个整数是否是2的N次幂实现方法
2022-12-25 00:55:10
springboot多租户设计过程图解
2021-12-21 08:09:45
![](https://img.aspxhome.com/file/2023/1/85521_0s.png)
java生成随机数的方法
2023-12-12 12:49:51
![](https://img.aspxhome.com/file/2023/4/65354_0s.jpg)
教你如何使用Java实现WebSocket
2021-08-22 20:39:02
![](https://img.aspxhome.com/file/2023/2/94372_0s.png)
教你怎么用Java开发扫雷游戏
2023-07-22 09:49:26
![](https://img.aspxhome.com/file/2023/0/63480_0s.png)
java 实现文件复制和格式更改的实例
2023-10-21 08:07:49
详解三种java实现多线程的方式
2021-11-02 10:09:30
Android刮刮卡功能具体实现代码
2021-08-06 04:12:24
![](https://img.aspxhome.com/file/2023/0/88670_0s.jpg)
Java操作FTP实现上传下载功能
2021-12-07 18:35:04
![](https://img.aspxhome.com/file/2023/1/82311_0s.png)
c#之事件用法
2023-09-22 05:05:28
DevExpress根据条件设置GridControl RepositoryItem是否可编辑
2023-03-21 14:53:30
![](https://img.aspxhome.com/file/2023/0/92960_0s.png)
详解Java中的泛型
2021-12-02 14:57:34
![](https://img.aspxhome.com/file/2023/4/83564_0s.png)
jcrop 网页截图工具(插件)开发
2022-10-21 22:30:19
![](https://img.aspxhome.com/file/2023/4/86854_0s.png)
c#队列Queue学习示例分享
2022-06-30 13:39:45
java实现模仿斗地主发牌
2021-07-21 07:45:11
浅析java实现数据加密问题
2023-03-21 02:54:04
Spring Security中用JWT退出登录时遇到的坑
2022-05-19 10:30:28
![](https://img.aspxhome.com/file/2023/1/70921_0s.jpg)
Java毕业设计实战之药店信息管理系统的实现
2022-03-07 11:55:37
![](https://img.aspxhome.com/file/2023/2/72522_0s.jpg)
Java实现二叉树的深度优先遍历和广度优先遍历算法示例
2021-07-16 12:37:17
![](https://img.aspxhome.com/file/2023/7/84177_0s.jpg)
Springboot Session共享实现原理及代码实例
2022-12-16 03:29:52
![](https://img.aspxhome.com/file/2023/2/66072_0s.png)