Jay
Jay

Reputation: 321

System cannot find dll

I have a dll called test and within test.dll, I'm referencing another dll called process. Now when I try loading test.dll, I get the error "System cannot find process.dll. Please help

Assembly u = Assembly.LoadFile(@"C:\test\test.dll");

        Type t = u.GetType("Test.Process");
        MethodInfo m = t.GetMethod("ProcessFile");

        try
        {
            object[] myparam = new object[1];
            myparam[0] = @"C:\test\testFile.csv";

            result = (string)m.Invoke(null, myparam);

            Console.WriteLine(result);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message.ToString());
            Console.WriteLine(ex.InnerException.ToString());
            System.Threading.Thread.Sleep(100000);
        }

Upvotes: 0

Views: 998

Answers (3)

jlvaquero
jlvaquero

Reputation: 8785

Get test.dll fileInfo:

 FileInfo fileInfo = new FileInfo("test.dll");

and use fullName to load the assembly:

Assembly assem = Assembly.LoadFile(fileInfo.FullName);

I hope it helps.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

Use LoadFrom instead of LoadFile. Quote from the documentation:

Use the LoadFile method to load and examine assemblies that have the same identity, but are located in different paths. LoadFile does not load files into the LoadFrom context, and does not resolve dependencies using the load path, as the LoadFrom method does. LoadFile is useful in this limited scenario because LoadFrom cannot be used to load assemblies that have the same identities but different paths; it will load only the first such assembly.

Assembly u = Assembly.LoadFrom(@"C:\test\test.dll");
...

Upvotes: 4

Jon Skeet
Jon Skeet

Reputation: 1499760

I suspect you want LoadFrom instead of LoadFile in this case. The difference is that the extra path (c:\test) will be added to the "load from" context which will then be used for dependencies such as process.dll.

At the moment, it's trying to resolve process.dll without taking c:\test into consideration. Read the linked documentation for more information.

Upvotes: 3

Related Questions