tags:

views:

696

answers:

4

Hi,

In C# console application we can add dll reference by [Soln Explorer - References & then adding reference of reqd dll].

In that case,the application expects the dll to be present in same folder. if not, then application doesn't work[Throws exception].

Is it possible that, if a dll in not present in same folder as that of application[exe] , then can we have some other alternative location\path from where the program can load reqd dll then continue with its functionality.

Thanks, Amit

A: 

You can install the library in the GAC (Global Assembly Cache).

Fredrik Jansson
What if i have to specify some location on hard disk, without installing it into the GAC?
Amit
A: 

You could put all the DLLs you wish to reference in a Libraries folder or some such and reference them from there.

Project > References > Browse

Kindness,

Dan

Daniel Elliott
Dan,is there a way to reference those dll through code.
Amit
+3  A: 

As answered by Fredik you can install the assembly in GAC. There is another way to do this. You can copy the assembly to any location (Make this location as trusted, if it is on network) and handle ModuleResolve event of the type Assembly. In the callback you then have a chance to resolve the reference to the assembly.


On second thought, I would instead suggest you to look at AssemblyResolver. It should definitely solve any issues you have with resolving assemblies.

P.K
I would be thankful to u, if u could provide me with the code snippet for this
Amit
Take a look at this link:http://msdn.microsoft.com/en-us/library/system.reflection.moduleresolveeventhandler%28VS.71%29.aspx<br/>But I would suggest that you take a look at darin's answer
P.K
+1  A: 

You could add App.config file to your application and specify the location of the assembly:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly> 
            <assemblyIdentity 
                name="MyAssembly" 
                culture="" 
                publicKeyToken="8968ee41e78ce97a" /> 
            <codeBase 
                version="1.0.0.0" 
                href="file://c:/some_path/myassembly.dll" />
        </dependentAssembly>
    </assemblyBinding>
</runtime>


EDIT:

  1. Might need to set the culture="neutral"
  2. Verify that the token is correct if the assembly is strongly signed otherwise set publicKeyToken="null"
Darin Dimitrov
This is how you should do it. Only in rare circumstances should you use ModuleResolve as suggested in my answer.
P.K
<?xml version="1.0" encoding="utf-8" ?><configuration><runtime><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentityname="Interop.Microsoft.Office.Interop.Excel"culture=""publicKeyToken="" /><codeBaseversion="1.0.0.0" href="file://C:/Dll_Path/Interop.Microsoft.Office.Interop.Excel.dll" /></dependentAssembly></assemblyBinding></runtime></configuration>when i added this content. I got compile time error.Error 2 Application Configuration file "App.config" is invalid. Some attributes of the assemblyIdentity element are incorrect
Amit
Is your assembly strongly signed? If yes, verify that you entered the publicKeyToken correctly, otherwise set it to "null". Also you might need to set the culture to "neutral". You could use Reflector to get these properties.
Darin Dimitrov
No its not strongly named assembly.When i searched on net, i came to know that you can only sign dll/assemblies whose source code is with you. Can you tell me how can i sign already existing dll. e.g. one which is provided by microsoft?
Amit
Its basically a COM dll for Excel interop operation
Amit