2

I have written a C# application which uses an external .dll, which is loaded at runtime. The application works fine, if I copy this .dll to the bin directory of my application. Since also other applications use the external .dll I want to have it only once on the system, such that every application uses the same .dll.

So I added the corresponding path to the PATH-Environment variable.

When I delete the .dll from my bin directory and start the application I get a

System.TypeLoadException: Failed to resolve the Type

The way proposed in Visual Studio: how to set path to dll? does not work

Community
  • 1
  • 1
Sebastian
  • 23
  • 5

2 Answers2

0

Adding the assembly location to the PATH variable won't make it available to your application because that's not how .NET assembly lookup is performed.

If you want the same assembly to be used by several applications, you'll need to add it to the Global Assembly Cache (GAC). You should note that in order to do that you'll need to sign your assembly. For more information on how to add your assembly to the GAC, you can have a look here.

Adi Lester
  • 24,731
  • 12
  • 95
  • 110
  • I forgot to mention: The external .dll is loaded at runtime. Does this mean then, if I want to install the application on a system, which does not have VS installed, I actually need an installer for the external .dll? – Sebastian Sep 18 '13 at 10:57
  • If by loaded at runtime you mean that you call `Assembly.LoadFrom`, then you don't need to add it to the GAC, all you have to do is know its location on disk. If it's not the case and you want it to be in a shared directory which is not the app directory, your installer will need to add it to the GAC. – Adi Lester Sep 18 '13 at 11:02
0

If it is just your own machine there are a couple of ways if you don't want to use CopyLocal or GAC.

  • Create a symbolic link to the dll in the bin folder

MKLINK bin\mydll.dll C:\path\to\dlls\mydll.dll

  • Use the DEVPATH environment variable to specify where assemblies are located

Add the following to the config file (or machine.config if you want a global setting)

<configuration>
    <runtime>
        <developmentMode developerInstallation="true"/>
    </runtime>
</configuration>

And then create the environment variable DEVPATH

SET DEVPATH=C:\path\to\dlls;C:\Path\to\somewhereelse

Just ensure DEVPATH contains at least one valid path. Otherwise all .Net programs will crash on startup with a strange error message.

adrianm
  • 14,468
  • 5
  • 55
  • 102