Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing a project which requires to use some DLLs. I have some DLLs in my project Release folder. But for one of the DLL location is not fixed. i.e. it can change on different PC where I am running my application. Location of that DLL is available in Environment Variable say "TEMP". So now I want to refer to %TEMP%/mydll.dll . How can I refer to this dll in C#.net?

Thanks in Advance
Rohit
Posted

Referencing the assembly during development and using it during run time are different things. When you build the solution, the referenced assembly should be there to allow the build. If you deploy it and place it in the same directory as the referencing assembly, it will work just fine.

What to do if you don't want to deploy this assembly but you want to require that it should present in the target system, by some reason (by the way, why?!)? Environment variable as such won't help you, but in principle, you can use it during installation.

There are generally two ways of resolving the referenced assembly. The first one is: you can require it to be placed in GAC. Everyone can do it using cagutil.exe. How to require it is your problem, but if it is formally submitted to GAC (you need to sign it just for this purpose as the strongly named assembly is required), it will always be resolved properly. It also helps unique versioning (strong naming, not GAC itself).

Another way of resolving the referenced assembly is the application's standard configuration file. Here is the sample using assembly location probing:
XML
<configuration>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <probing privatePath=".\MyLibraries\MyAssemblies"/>
        </assemblyBinding>
    </runtime>
</configuration>


If you don't use this configuration file, the application will run, and the referenced assembly should be in the same directory. If you add this file and name it "myApplication.exe.config" (where "myApplication.exe" is the name of the main executable module of the application entry assembly), the run-time will try to find the required referenced assembly in the sub-directory specified by privatePath, in this example, relative to the location of this executable module. This way, you can reuse a set of referenced between applications located in different directories. You can develop a deployment procedures writing appropriate configuration files with required locations of directories for probing, depending on user's optional parameters or other factors.

—SA
 
Share this answer
 
If you create a reference to the dll using Add Reference dialogue in Solution Explorer, the dll is automatically copied to the same folder as the exe file. So if your exe is in yourproject\bin\release, you will find a copy of all local assemblies referenced by the project in the same folder.

Hope this helps
 
Share this answer
 
Comments
Rohit_patel 17-Jan-12 1:08am    
So if I want to ship my application to 3rd party, I have to ship that dll also with package? If that dll is already there in other PC, Do I need to ship it? Can't it identify location on PC?
Wayne Gaylard 17-Jan-12 1:17am    
It is generally shipped with the application and included in the deployment / build application. This is meant to solve version issues with different dll versions. The only other option is if the dll is installed in the global assembly cache.
Rohit_patel 17-Jan-12 1:20am    
Thanks Wayne for your valuable suggestions..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900