Specifying a non-.NET dependency in Visual Studio

2019-02-21 19:27发布

问题:

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?

回答1:

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).



回答2:

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.



回答3:

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.