Andrew Ducker
Andrew Ducker

Reputation: 5500

Trying to load an app through reflection and get error "Could not load file or assembly...The system cannot find the file specified."

The assembly it's trying to find isn't the root assembly - it's a referenced one, but it's in the same folder, and Directory.GetCurrentDirectory() is the folder with all of the files in.

I'm stuck - any suggestions?

Upvotes: 0

Views: 2630

Answers (4)

Josué Zatarain
Josué Zatarain

Reputation: 868

Executing the program from the folder that has the referenced dll can also solve the problem.

Upvotes: 0

Kent Boogaart
Kent Boogaart

Reputation: 178760

You can either:

  1. Create a new AppDomain to load the assembly (and set the AppDomain's base directory to the directory containing all the assemblies).
  2. Attach a handler for AppDomain.AssemblyResolve to help the CLR find the assembly's dependencies.
  3. You might be able to add the directory in question to the list of paths to probe. However, it will need to reside somewhere under your application's directory. See the probe element for more info.

Upvotes: 3

Aaron Fischer
Aaron Fischer

Reputation: 21231

If you use assembly.loadfrom you can specify the file path to the assembly.

The load-from context allows an assembly to be loaded from a path not included in probing, and yet allows dependencies on that path to be found and loaded because the path information is maintained by the context.

Upvotes: 0

Chris Marisic
Chris Marisic

Reputation: 33108

You could 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 physical directory in which the assemblies are running. This could be in the Windows .NET temporary directories. However, because the files are at the same level they should exist there side by side.

Upvotes: 0

Related Questions