在 .NET 项目中复制本机 DLL 的最简洁方法
我有一个引用托管 C++ 项目的 C# GUI 应用程序,该项目需要 7 个本机 C++ DLL。我正在寻找将这 7 个 DLL 复制到最终项目输出的最干净的方法。
什么有效
将所有 DLL 添加到 C# 应用程序,并指定:
构建操作==“内容”
复制到输出目录 == 始终复制”
在某些情况下,这将使项目的基本文件夹变成一堆 DLL,所有这些都是引用项目的要求,而不是该项目本身。
什么不起作用
- 具有相同名称的文件夹中,导致它们位于不正确的位置。指定输出目录的方法
- :在 C# P/Invoke 中,您可以添加引用为嵌入式资源的 DLL,并且这些 DLL 嵌入在您的最终库中,我在托管 C++ 中看不到这种可能性。我什至不确定它是否适用于
- 托管 C++ 项目中的内容。
在这种情况下,我更喜欢托管的解决方案是什么?如果可能的话,C++ 项目能够处理它自己的 DLL 要求,并且最好以不会阻止项目跨多个应用程序使用的方式。
就拥有一个干净的项目而言,将所有代码文件插入项目的子文件夹中并将 DLL 放在根目录下以使第一个解决方案正常工作是否更好?
解决方案:
使用 Joseph 的构建后建议,以下命令可以实现使用“所需 DLL”文件夹的技巧。
xcopy "$(ProjectDir)必需的 DLL*.*" "$(TargetDir)" /Q /Y
/Q 从输出中隐藏各个文件,/Y 抑制覆盖提示。
I have a C# GUI application that references a Managed C++ project, which requires 7 native C++ DLLs. I'm looking for the cleanest method for copying these 7 DLLs to the final project output.
What works
Add all DLLs to the C# applications, specifying:
Build Action == "Content"
Copy To Output Directory == Copy Always"
This will make the base folder of the project a mess of DLLs in some cases, all of which are requirements of referenced projects, and not that project itself.
What does not work
- Adding these DLLs to a folder named "Required DLLs" with the above settings. It copies it to a folder with the same name in the output, causing them to be in an incorrect location. I can't see a way to specify the output directory.
- Embedded Resources: In C# P/Invoke, you can add DLLs you're referencing as embedded resources, and the DLLs are embedded inside your final library. I don't see this possibility in Managed C++, and I'm not even sure if it works with reference chains.
- Adding the DLLs as content within the Managed C++ project. The files do not get copied to the output directory.
What is the best solution in this case? I'd prefer the Managed C++ project to be able to handle it's own DLL requirements if possible, and preferably in a way that won't prevent the project from being used across multiple applications.
As far as having a clean project goes, is it better to insert all my code files within subfolders in the project, and have the DLLs at the root to make the first solution work?
Solution:
Using the post-build suggestion from Joseph, the following command does the trick to use a "Required DLLs" folder.
xcopy "$(ProjectDir)Required DLLs*.*" "$(TargetDir)" /Q /Y
/Q hides the individual files from the output, and /Y suppresses overwrite prompts.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用构建后事件将目录(例如,“所需的 DLL”目录)的内容复制到项目的输出目录中。
You can use a post-build event to copy the contents of a directory (e.g., your "Required DLLs" directory) into the project's output directory.