溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C#怎么把dll分別放在指定的文件夾

發(fā)布時間:2022-05-19 08:56:39 來源:億速云 閱讀:296 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“C#怎么把dll分別放在指定的文件夾”的相關(guān)知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“C#怎么把dll分別放在指定的文件夾”文章能幫助大家解決問題。

C#客戶端程序,生成后是一個exe,如果帶有大量的dll,那么dll和exe會混亂在一起,看起來非常混亂,我們可以建立一個文件夾,把dll放進去,這樣看起來就非常的清晰美觀。

一共有二種方法

第一種,配置方法。

1.我們建立一個winform程序,對2個dll分別引用,調(diào)用里面的方法

C#怎么把dll分別放在指定的文件夾

生成后的文件是這樣的

C#怎么把dll分別放在指定的文件夾

2.打開App.config文件夾,其中dll和dll/2相當(dāng)于文件夾

<?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,把復(fù)制本地設(shè)置成 FALSE

C#怎么把dll分別放在指定的文件夾

4.打開項目的exe路徑,分別建立dll文件夾,把其中一個dll放進去 

C#怎么把dll分別放在指定的文件夾

建立dll/2文件夾,把另一個dll放進去

C#怎么把dll分別放在指定的文件夾

C#怎么把dll分別放在指定的文件夾

5.文件夾的效果

WindowsFormsApp4.exe

WindowsFormsApp4WindowsFormsApp4.exe.config

dll

...../ClassLibrary1.dll

...../2/ClassLibrary2.dll

6.效果,這樣就比較好看一些。

C#怎么把dll分別放在指定的文件夾

第二種,代碼方法

 1.同樣建立一個項目,選擇所有的dll,把復(fù)制本地設(shè)置成 FALSE

C#怎么把dll分別放在指定的文件夾

2.在窗體的初始化出寫入

C#怎么把dll分別放在指定的文件夾

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扔進去即可。

C#怎么把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失敗時調(diào)用
        /// </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);
        }
    }
}

關(guān)于“C#怎么把dll分別放在指定的文件夾”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

dll
AI