8

I'm calling a non-.NET dll from my project using P/Invoke, meaning that the .dll must always be present in the .exe's directory.

Is there any way to tell Visual Studio of this dependency, so that it will automatically copy the .dll to the output directory when compiling, and will automatically include the .dll in the setup? Or do I have to do this manually?

Grokys
  • 16,228
  • 14
  • 69
  • 101

3 Answers3

8

You can simply add the .DLL to your project.

Select the Properties pane for that file and set Build Action to Content and Copy to Output Directory to Copy if newer.

Magnus Johansson
  • 28,010
  • 19
  • 106
  • 164
  • Build Action = "Content" is better than Build Action = "None" suggested in another response. This is because you can easily include "Content" files in a Setup project. – Joe Sep 24 '08 at 11:02
  • Yes, and that is exactly why I suggested that in my response. – Magnus Johansson Sep 24 '08 at 12:28
  • > that is exactly why I suggested that in my response And why I upvoted your answer:) – Joe Sep 24 '08 at 16:48
5

You can copy/link this file(s) to the project, and in properties windows set "Build Action" to "None" and "Copy to Output Directory" to "Copy if newer" or "Copy always".

Or you can use a "Pre-Build Events" & "Post-Build Events" where you can specify any batch scripts.

I prefere the second option, because this way is more flexible than the first.

Also you can modify a MSBuild file and add a task for copy the file(s).

TcKs
  • 25,849
  • 11
  • 66
  • 104
2

I think one problem with just adding a .DLL to the project is that you may need different versions of a DLL for debug and release builds. You'd think you can add both debug and release versions of the DLL to the file, and based on configurations, exclude the inappropriate one, but I couldn't find a way to do that. I'm using Visual Studio 2010. I am positive this worked in the old days with VS6.

zumalifeguard
  • 8,648
  • 5
  • 43
  • 56
  • You can manually edit the .csproj file to add a Condition to the DLL's content node: Alternatively, you could use a pre-build step to copy either MyDLL.Debug.dll or MyDLL.Release.dll to MyDLL.dll depending on the current configuration. (I think you should be able to do this through the Project properties page in VS, by switching from Debug to Release) – bruceboughton Feb 09 '10 at 10:42