Attempting to load the application through reflection and get the error "Failed to load file or assembly ... The system cannot find the file specified."

The assembly it is trying to find is not the root assembly - it's called a link, but it's in the same folder, and Directory.GetCurrentDirectory () is the folder with all the files.

I'm stuck - any suggestions?

0


source to share


4 answers


You can:



  • Create a new one AppDomain

    to load the assembly (and set the base directory AppDomain

    to the directory containing all assemblies).
  • Attach a handler for AppDomain.AssemblyResolve

    to help the CLR find assembly dependencies.
  • You may be able to add the directory in question to the probing path list. However, it will need to be located somewhere in your application directory. See probe for more information .
+3


source


You can try using something like this

string myDll = string.Empty;
string location = Assembly.GetExecutingAssembly().Location;
if (location != null)
{
    myDll = string.Format(@"{0}\my.assembly.name.dll", location.Substring(0, location.LastIndexOf(@"\")));
}

      



This should get the physical directory where the assemblies are running. It could be in Windows.NET temporary directories. However, since the files are at the same level, they must exist next to each other.

0


source


If you are using assembly.loadfrom you can provide the path to the assembly file.

The load-from context allows an assembly to be loaded from a path not probed, and still allows dependencies on that path and loaded because the path information is supported by the context.

0


source


Executing the program from the folder referenced by the dll can also fix the problem.

0


source







All Articles