有没有更好的方法将本机 dll 复制到 bin 文件夹? [英] Are there any better ways to copy a native dll to the bin folder?

查看:43
本文介绍了有没有更好的方法将本机 dll 复制到 bin 文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有从本机 (C++) dll 调用函数的 C# 包装器代码.目前,我可以添加对 C# dll 的引用并将复制本地"选项设置为 true.但是,作为依赖项的本机 dll 无法添加为引用 - 因此没有复制本地"选项.

I have C# wrapper code that calls functions from a native (C++) dll. Currently, I can add a reference to the C# dll and have set the 'Copy Local' option to true. However the native dll, which is a dependency, cannot be added as a reference - so there is no 'Copy Local' option.

我尝试了以下方法

  1. 使用构建后事件将本机 dll 从 Libs 文件夹复制到 $(TargetFolder)

copy "$(ProjectDir)LibsNQuantLibc.dll" "$(TargetDir)NQuantLibc.dll"

将本机 dll 作为现有项目包含在项目中(添加 -> 现有项目 -> 包含 dll).此选项允许我使用复制本地"选项.这种方法的缺点是 dll 始终显示为项目项.

Included the native dll as an existing item in the project (Add -> Existing Item -> Include dll). This option allows me to use the 'Copy Local' option. The downside to this approach is that the dll always shows as a project item.

我还尝试了显示所有文件",这让我可以看到 Libs 文件夹.然后我在项目中包含 NQuantLibc.dll 文件,这允许我设置复制本地"选项.然而,这却给了我意想不到的结果.它在 bin 文件夹中创建了一个包含 dll 的 Libs 子文件夹(例如 bin/debug/Libs/NQuantLibc.dll).不理想,因为 C# dll 无法正确调用本机 dll,因为它不存在.

I also tried "Show All Files" which allowed me to see the Libs folder. I then include the NQuantLibc.dll file in the project which allowed me to set the 'Copy Local' option. However, this gave me a unexpected result. It created a Libs subfolder containing the dll within the bin folder (eg bin/debug/Libs/NQuantLibc.dll). Not ideal since the C# dll was not able to properly call the native dll since it was not there.

上述两个选项都有效.有没有更好的方法将本机 dll 复制到 bin 文件夹,以便始终解决依赖关系?或者,是否有针对此类场景的不同方法?

Both of the above options above work. Are there any better ways to copy a native dll to the bin folder such that the dependency will always be resolved? Alternatively, is there a different approach to this type of scenario?

推荐答案

Use Project + Add Existing Item 并选择 DLL.在解决方案资源管理器窗口中选择添加的文件.在属性"窗口中,将复制到输出目录"设置更改为如果更新则复制".

Use Project + Add Existing Item and select the DLL. Select the added file in the Solution Explorer window. In the Properties window, change the Copy to Output Directory setting to "Copy if newer".

这篇关于有没有更好的方法将本机 dll 复制到 bin 文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆