C#把dll分別放在指定的文件夾的方法步驟
C#客戶端程序,生成后是一個(gè)exe,如果帶有大量的dll,那么dll和exe會(huì)混亂在一起,看起來非?;靵y,我們可以建立一個(gè)文件夾,把dll放進(jìn)去,這樣看起來就非常的清晰美觀。
一共有二種方法
第一種,配置方法。
1.我們建立一個(gè)winform程序,對(duì)2個(gè)dll分別引用,調(diào)用里面的方法
生成后的文件是這樣的
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
4.打開項(xiàng)目的exe路徑,分別建立dll文件夾,把其中一個(gè)dll放進(jìn)去
建立dll/2文件夾,把另一個(gè)dll放進(jìn)去
5.文件夾的效果
WindowsFormsApp4.exe
WindowsFormsApp4WindowsFormsApp4.exe.config
dll
...../ClassLibrary1.dll
...../2/ClassLibrary2.dll
6.效果,這樣就比較好看一些。
第二種,代碼方法
1.同樣建立一個(gè)項(xiàng)目,選擇所有的dll,把復(fù)制本地設(shè)置成 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.在項(xiàng)目的debug文件夾中,建立代碼中的名字dll2文件夾,把所有的dll扔進(jìn)去即可。
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> /// 對(duì)外解析dll失敗時(shí)調(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分別放在指定的文件夾的方法步驟的文章就介紹到這了,更多相關(guān)C# dll指定文件夾內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#實(shí)現(xiàn)輸入10個(gè)數(shù)存入到數(shù)組中并求max和min及平均數(shù)的方法示例
這篇文章主要介紹了C#實(shí)現(xiàn)輸入10個(gè)數(shù)存入到數(shù)組中并求max和min及平均數(shù)的方法,涉及C#簡(jiǎn)單數(shù)據(jù)轉(zhuǎn)換與數(shù)值運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2017-07-07C#使用BinaryFormatter類、ISerializable接口、XmlSerializer類進(jìn)行序列化和反序列
這篇文章介紹了C#使用BinaryFormatter類、ISerializable接口、XmlSerializer類進(jìn)行序列化和反序列化的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09C# 中將數(shù)值型數(shù)據(jù)轉(zhuǎn)換為字節(jié)數(shù)組的方法
C# 中將數(shù)值型數(shù)據(jù)轉(zhuǎn)換為字節(jié)數(shù)組的方法,需要的朋友可以參考一下2013-05-05DataTables List互相轉(zhuǎn)換的實(shí)現(xiàn)類示例
這篇文章主要介紹了將DataTable轉(zhuǎn)換為L(zhǎng)ist,將List轉(zhuǎn)換為DataTable的實(shí)現(xiàn)類實(shí)例方法,大家參考使用吧2013-11-11C#利用微軟自帶庫(kù)進(jìn)行中文繁體和簡(jiǎn)體之間轉(zhuǎn)換的方法
這篇文章主要介紹了C#利用微軟自帶庫(kù)進(jìn)行中文繁體和簡(jiǎn)體之間轉(zhuǎn)換的方法,涉及C#使用Microsoft.VisualBasic類庫(kù)操作中文繁簡(jiǎn)字體轉(zhuǎn)換的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04