开发者

load a DLL reference from a different folder?

开发者 https://www.devze.com 2022-12-15 08:01 出处:网络
I have a .NET console app that references a DLL.It runs fine if the DLL is in the same folder as the EXE but I would like to put the DLL in a different folder.How can I do that开发者_Python百科?If the

I have a .NET console app that references a DLL. It runs fine if the DLL is in the same folder as the EXE but I would like to put the DLL in a different folder. How can I do that开发者_Python百科?


If the DLL is in a sub folder you can add this folder to the AppDomain private path.

The private bin path of an AppDomain cannot be changed once the AppDomain has been created (AppDomain.AppendPrivatePath is obsolete), if you don't want to create a new AppDomain, you can modify the probing element in the App.config.

If it's not in a sub folder, things get more complicated, you can load the assembly using its full path, but if it references other assembly, the CLR won't be able to resolve the dependencies.

If you want to resolve dependencies, you can add an handler to AppDomain.AssemblyResolve and fetch the needed assembly in your specific folder.

Another possibility is to place this DLL (and dependencies) in the GAC.


If the DLL is loaded dynamically, just specify the full path to it. If it's static, or you feel like it, add the library path to the PATH environment variable. Yes, this DOS relic is still around.

Also, read the doc on the LoadLibrary() API for a better understanding of DLL loading.


Well you could have the application look in certain directories that you can specify in a configuration section. Then you could load them up at the start of your application. I don't really know what you are trying to do, but this could be a solution.

Assembly.Load("Assembly.Name")

If you aren't building some kind of plugin architecture, I would think about why you have to do this.

0

精彩评论

暂无评论...
验证码 换一张
取 消