iis服务器助手广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >C#把dll分别放在指定的文件夹的方法步骤
  • 688
分享到

C#把dll分别放在指定的文件夹的方法步骤

2024-04-02 19:04:59 688人浏览 泡泡鱼
摘要

目录第一种,配置方法。第二种,代码方法C#客户端程序,生成后是一个exe,如果带有大量的dll,那么dll和exe会混乱在一起,看起来非常混乱,我们可以建立一个文件夹,把dll放进去

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);
        }
    }
}

到此这篇关于C#把dll分别放在指定的文件夹的方法步骤的文章就介绍到这了,更多相关C# dll指定文件夹内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: C#把dll分别放在指定的文件夹的方法步骤

本文链接: https://www.lsjlt.com/news/149124.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作