C# 通過 probing 指定 dll 尋找資料夾
在很大的專案開發,會發現專案引用的 dll 會很多,我想要按照不同的功能,將不同的 dll 放在不同的資料夾
簡單的方法是通過修改 App.config 檔案指定資料夾,如將檔案移動到abc\12
的資料夾裡面,可以在 App.config 新增程式碼
<?xml version="1.0" encoding="utf-8" ?> <configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <probing privatePath="abc\12" /> </assemblyBinding> </runtime> </configuration>
如建立一個簡單的專案,此時專案引用一個dll 如 doubi.dll 這個專案執行的時候輸出的檔案有 lindexi.exe 和 doubi.dll 檔案
這時需要將 doubi.dll 移動到資料夾abc\12
裡面
lindexi.exe abc\12\doubi.dll
開啟 App.config 新增上面的程式碼就可以
如果有兩個不同的dll需要放在兩個不同的資料夾,如 walter.dll 需要放在 walter 資料夾
在<probing privatePath="abc\12" />
裡面使用分號表示不同的資料夾probing privatePath="abc\12;walter"
不同的資料夾之間用分號分開
<?xml version="1.0" encoding="utf-8" ?> <configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <probing privatePath="abc\12;walter" /> </assemblyBinding> </runtime> </configuration>
這個 App.config 在編譯之後會在被修改為程式集名.exe.config
在輸出資料夾找到xx.exe.config
可以通過修改這個檔案在編譯之後修改 dll 的尋找資料夾
如果是對於 C++ 的 dll 需要做特殊引用,如需要區分 x86 和 x64 請看ofollow,noindex" target="_blank">C# 如何在專案引用x86 x64的非託管程式碼
不能直接新增一個 x86 檔案和一個 x64 資料夾,通過 privatePath 同時指定資料夾的方式