How can I modify assembly search path from the code?

Posted on 2007-07-24
Medium Priority
Last Modified: 2012-08-13
Dear expert.
I have a C# assembly module.dll that has a reference to C# submodule.dll.
module.dll has a public class module.ClassInvoker that has a public static method Invoke.
Method Invoke uses code from submodule.dll.
Both of the modules are located in D:\DOTNETSamlpe\lib.

I have DOT net application that has no reference to module.dll and calls to method dynamically:
System.Reflection.Assembly asm = System.Reflection.Assembly.LoadFile(""D:\\DOTNETSamlpe\\lib\\module.dll"");
 System.Type classType = asm.GetType("module.ClassInvoker");
 System.Reflection.MethodInfo  methodInfo = classType.GetMethod("Invoke");  
 methodInfo.Invoke(null, null);

This code works only if exe is located in D:\DOTNETSamlpe\lib  the same folder where module.dll and submodule.dll are located.

If exe is not located in D:\DOTNETSamlpe\lib methodInfo.Invoke(null, null) throws exeption. I see that inner exception is Could not load file or assembly 'submodule, Version=, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.":"submodule, Version=, Culture=neutral, PublicKeyToken=null

I suspect that the problem is that the search path doesnt match in D:\DOTNETSamlpe\lib.
How can I modify assembly search path from the code?
Question by:netformx
  • 2
  • 2
LVL 18

Expert Comment

ID: 19554579

Can you install the assemblies in the GAC? Then the would be found.

Just a thought.


Author Comment

ID: 19554738
I want it work when it is not located in GAC.
LVL 18

Accepted Solution

DarrenD earned 2000 total points
ID: 19554769

What about trying to use LoadFrom instead of LoadFile.

What version of .NET are you using?

Just another thought.


Author Comment

ID: 19554899
Thank a lot

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question