Call DLL files located in another directory

2019-07-23 18:14发布

I have a console application (NGameHost) running in a specific directory (C:\Program Files\NetworkGame3\api\). It uses the files available in that directory and the console application works well when run on its own. It also exposes various methods that use the DLLs (and other files such as config files) from that directory. I now have another console application (located elsewhere) that tried to call those methods and return the results. I've set Copy Local: False so that it executes within that directory instead of creating a local version. However I get the error "Could not load file or assembly ... or one of its dependencies. The system cannot find the file specified."

How can I call the methods from a console application located in another directory?

3条回答
【Aperson】
2楼-- · 2019-07-23 18:44

I've recently been bashing my head against a brick wall trying to resolve a very similar problem. I've a DLL I want to load at runtime, and it has it's own dependencies. I couldn't for the life of me figure out why it would load, but when calling a class's constructor in the loaded assembly, that no error was raised, and execution simply stalls, leaving you wondering what's going on.

Turns out that a dependency of the loaded assembly was in a different .NET version, and there is an App.Config on that code to enable mixed mode assemblies. So I naturally had to bring that into my code too, as I'm calling an assembly that calls an assembly in a different version of .NET.

The error didn't present itself until I copied all the dependent's DLL's to my application in development. Now I can remove them again! In doing so, I get a DLL Loader Lock warning message. If I suppress it or ignore it, my code works.

In my case then, the resolution was :

<?xml version="1.0"?>
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0"/>
  </startup>
</configuration>
查看更多
不美不萌又怎样
3楼-- · 2019-07-23 18:56

When you set copy local = false what you're saying is that do not copy the assembly to the local directory in which case assembly will available in one of the places where the runtime looks for it.

See How the runtime locates assembly

Your assembly has to be either in GAC or in one of the probing locations.

查看更多
Deceive 欺骗
4楼-- · 2019-07-23 19:01

You can use the GAC, a config approach or an assembly resolve event.

This KB covers it in more detail:

http://support.microsoft.com/kb/837908

Also look into probing paths:

http://msdn.microsoft.com/en-us/library/15hyw9x3(v=vs.71).aspx

查看更多
登录 后发表回答