How can I modify assembly search path from the code?

Posted on 2007-07-24
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
    LVL 18

    Expert Comment


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

    Just a thought.


    Author Comment

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

    Accepted Solution


    What about trying to use LoadFrom instead of LoadFile.

    What version of .NET are you using?

    Just another thought.


    Author Comment

    Thank a lot

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    In my previous two articles we discussed Binary Serialization ( and XML Serialization ( In this article we will try to know more about SOAP (Simple Object Acces…
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now