C#把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指定文件夹内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • C#如何通过probing指定dll寻找文件夹详解

    前言 我们在很大的项目开发,会发现项目引用的 dll 会很多,我想要按照不同的功能,将不同的 dll 放在不同的文件夹 简单的方法是通过修改 App.config 文件指定文件夹,如将文件移动到 abc\12 的文件夹里面,可以在 App.config 添加代码 <?xml version="1.0" encoding="utf-8" ?> <configuration> <runtime> <assemblyBinding

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

    目录 第一种,配置方法. 第二种,代码方法 C#客户端程序,生成后是一个exe,如果带有大量的dll,那么dll和exe会混乱在一起,看起来非常混乱,我们可以建立一个文件夹,把dll放进去,这样看起来就非常的清晰美观. 一共有二种方法 第一种,配置方法. 1.我们建立一个winform程序,对2个dll分别引用,调用里面的方法 生成后的文件是这样的 2.打开App.config文件夹,其中dll和dll/2相当于文件夹 <?xml version="1.0" encoding=&

  • linux中tar打包指定路径文件的实现方法

    压缩: tar czvf /data/backup/test.tar.gz /data/a/b/directory 解压: cd /data/test tar xzvf /data/backup/test.tar.gz 问题是,解压后的文件,在/data/test/data/a/b/directory里面 要想解压在当前目录路径. 这样写就可以解决了 tar czvf /data/backup/test.tar.gz /data/a/b/directory 改成 tar czvf /data/b

  • PHP解压ZIP文件到指定文件夹的方法

    本文实例讲述了PHP解压ZIP文件到指定文件夹的方法.分享给大家供大家参考,具体如下: /** * function: 解压zip 格式的文件 * author:friker * date:2015-15-14 * reference:http://php.net/manual/zh/ref.zip.php * all rights reserved:wujiangwei123@126.com */ class Unzip{ public function __construct(){ //in

  • java删除指定目录下所有空文件夹的方法

    本文实例讲述了java删除指定目录下所有空文件夹的方法.分享给大家供大家参考,具体如下: package com.func; import java.io.File; import java.util.ArrayList; import java.util.List; /** * 删除指定目录下的所有空文件夹 * * @author zdw * */ public class FileUtils { List<File> list = new ArrayList<File>();

  • Python遍历指定文件及文件夹的方法

    本文实例讲述了Python遍历指定文件及文件夹的方法.分享给大家供大家参考.具体如下: 初次编写: import os def searchdir(arg,dirname,names): for filespath in names: open ('c:\\test.txt','a').write('%s\r\n'%(os.path.join(dirname,filespath))) if __name__=="__main__": paths="g:\\" os.

  • php递归删除指定文件夹的方法小结

    本文实例总结了两种php递归删除指定文件夹的方法.分享给大家供大家参考.具体如下: 方法一: function recursiveDelete($dir) { if ($handle = @opendir($dir)) { while (($file = readdir($handle)) !== false) { if (($file == ".") || ($file == "..")) { continue; } if (is_dir($dir . '/' .

  • 使用python os模块复制文件到指定文件夹的方法

    复制一个文件夹的文件到指定目录下 import os import shutil import time start_time = time.time() # 需要被复制的文件夹 old_path = r'D:\zjf_workspace\001-地标.利器.服饰\004文本\json1' new_path = r'D:\zjf_workspace\001-地标.利器.服饰\004文本\json' all_list = os.listdir(old_path) for i in all_list

  • 基于web项目log日志指定输出文件位置配置方法

    首先我们定义一个可以在运行时动态的找出项目的路径WebAppRootKey,这么做的原因是为了在后面配置log4j输出文件路径的时候能随心配置. <context-param> <param-name>webAppRootKey</param-name> <param-value>amt.root</param-value> </context-param> 然后要定义项目log配置文件的路径以及log4j监听器 <contex

  • 从Git上checkout指定的文件夹至本地的代码

    当项目过大时,从服务器上拉取项目是件很头疼的事情,那么就说说怎么只拉区某个或几个文件夹至本地. 上代码: git clone -n git@172.0.0.10:test/test_platform.git cd test_platform git config core.sparsecheckout true echo webapp/css >> .git/info/sparse-checkout git checkout master 如果需要 check out 多个文件夹的话,以此类推

  • shell脚本批量将文件复制到指定的文件夹下

    由于线上文件比较多,选择特定的文件拿下线下进行语料标注,如果指定的文件数量太多,一个个复制就很麻烦.所以写一个shell脚本进行批量操作. 首先把需要下载的文件路径写入txt文件中,如果需要路径补全,则在每条路径上加上*号,这样就很简单cd到对应的目录下,(就这个小点,花费了我很久时间) 如图所示: 然后就是遍历txt文件进行路径下操作, cat 2022-05-07_path.txt | while read line do #echo $line dir=根目录/"$line" e

随机推荐