views:

444

answers:

4

I am using EmguCV for a project and when our program runs it needs some dlls like "cxcore.dll" etc. (or it throws runtime exceptions). At the moment, I put the files in the root of the output folder (selected "Copy Always" in the file's properties in Visual Studio).

However it looks a bit messy, to have about 10 different dlls just there. Is there someway where I can move it to a subfolder in the output folder and it'll still find it.

A: 

You can copy the dll's to the place you want using the pre/post build events, and the macros that telling you where your output folder is.

But, if the dll's are not in the same directory as the executable is, they won't be loaded. If they are managed, you can load them manualy using the Assembly.Load methods. If they are unmanaged, I don't know how you can do it.

sagie
beware pre/post build events especially in environments where you are working with multiple devs and/or automated build. They can be big gotchas.
GrayWizardx
A: 

You can keep it elsewhere and still link it. In the reference properties, set the "Copy local" to false and set the path accordingly. This will work. If the external DLLS are suppose to change version, you can set "Specific version" to false to be able to link to any version.

Kangkan
+3  A: 

Amazing answers so far. None right ;) Well,

yes, you can put the assemblies in separate locations.

In the corresponding application config (yout.exe.config) add:

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <probing privatePath="lib" />
    </assemblyBinding>
  </runtime>

According to:

http://msdn.microsoft.com/en-us/library/823z9h8w.aspx

This will make the program look into the private path (folders under it's own folder) for assemblies - much like a web application looks for /bin.

You can also put them into the GAC, but that should be avoided unless there are other reasons for this.

That being said, you really dont need to. Users wont get confused if you install the application properly in the start menu ;) I never had that problem, including projects with 50+ assemblies. Users simlpy never see them.

TomTom
@TomTom Svish answer is right
Sergey Mirvoda
As for your answer. Are you sure it will work with unmanaged libs?
Sergey Mirvoda
@TomTom: Would the above be in my `.csproj` file? My project is a C# winforms application - I cannot find a `.config` file.
aip.cd.aish
Swish answer also was done after mine.The entry would go into the aplpications config file. Read up on .config files - really. This is .NET basics.Add itme, it will say application configuration. the result is a XML file named "app.config" that gets copied on built into "your.exe.config". This is where you can do runtime configuration - similar to (same file actually) the web.config in web applications. Read up on them.
TomTom
I am not sure this applies to libraries which are P/Invoked. It might work, and probably works by association, but I am not sure if it directly answers the question since we are talking about libraries not assemblies (i.e. c++ dlls)
GrayWizardx
He talks about assemblies. First line of the question. Unmanaged libs - no idea. I seriously wont even try moving the assmeblies, as I indicated - so I never tried that out.
TomTom
+3  A: 

To get the assemblies in a sub-directory you can copy them there manually, use a pre- or post-build event or something completely different.

To load them, you can use the AppDomain.AssemblyResolve Event, or (as noted by TomTom) the <probing> Element. From MSDN:

The following example shows how to specify application base subdirectories the runtime should search for assemblies.

<configuration>
   <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
         <probing privatePath="bin;bin2\subbin;bin3"/>
      </assemblyBinding>
   </runtime>
</configuration>

The GAC is of course another place to dump your assemblies, but that wouldn't really count as a sub-directory... unless you install your application somewhere it really shouldn't be installed :P

Svish
@Svish: Would the above be in my .csproj file? My project is a C# winforms application - I cannot find a .config file.
aip.cd.aish
@aip.cd.aish: There is a more complete example at http://msdn.microsoft.com/en-us/library/4191fzwb(v=VS.90).aspx. If there is no config file, you can just create one. According to http://msdn.microsoft.com/en-us/library/ms229689(v=VS.90).aspx it is supposed to be named the same as your application with a .config extension.
Svish
The file might also be named just app.config... but I'm not sure. Read about configuration files on MSDN and you should figure it out :)
Svish