简体   繁体   中英

How can I load a C# dll into a C# dll Project from a dynamic location?

The Program ClosedSourceProgram starts and copies my files into an unknown location. Their Structure will be:

?\project.dll
?\project\usefultools.dll

I have to load the usefultools.dll into my project but i dont know how i can tell C#VS2010 that the dll location will be

project\usefultools.dll

I added the library to my assembly as a reference so that its content is known during developement - therefor it tried to load earlier than i could load it via Assembly.LoadFrom() .

I manually copied the file for now into the ClosedSourceProgram-root directory. It now loads without Assembly.LoadFrom() .

current working tree:

?\ClosedSourceProgram\ClosedSourceProgram.exe
?\ClosedSourceProgram\usefultools.dll
?\ClosedSourceProgram\temp\project.dll

the tree i want:

?\ClosedSourceProgram\ClosedSourceProgram.exe
?\ClosedSourceProgram\temp\project.dll
?\ClosedSourceProgram\temp\project\usefultools.dll

I would like to tell my project that it should search subdirectories if it cant find the dll in the root directory - how can i do that?

You can load an assembly from an arbitrary location at runtime using Assembly.LoadFrom , and passing a filename.


Edit:

Another option is to subscribe to the AppDomain.AssemblyResolve event, and load the assemblies on demand. This has the advantage of allowing for any structure, since all undiscovered assemblies can loaded as needed by you.

您可以随时尝试Assembly.LoadFrom()

You can use the "~" operator if the assembly is with in any of the subfolders of the application path.

Assembly.LoadFrom("~\\project\\usefultools.dll");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM