gpt4 book ai didi

c# - Visual Studio 中自动复制 native dll 到引用项目的 bin 文件夹

转载 作者:太空宇宙 更新时间:2023-11-03 21:00:01 24 4
gpt4 key购买 nike

我有一些 native dll,必须根据平台条件将其复制到 bin 文件夹中。我希望将它们复制到引用该项目的项目的 bin 文件夹中。

如果我将构建操作设置为 Content,它们将被复制到 bin 文件夹,但文件夹结构保持不变,因此它们不会位于 bin 文件夹中,而是位于子文件夹中。所以在运行程序时,将无法解析dll,因为它们在子文件夹中。

例如,如果我在项目文件中有这段代码

<Choose>
<When Condition=" '$(Platform)'=='x86' ">
<ItemGroup>
<Content Include="nativedll\somelib\x86\somelib.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</When>
<When Condition=" '$(Platform)'=='x64' ">
<ItemGroup>
<Content Include="nativedll\somelib\x64\somelib.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</When>
</Choose>

如果是 x86,dll 将位于文件夹 bin\nativedll\somelib\x86\somelib.dll 中。

所以我尝试使用后期构建脚本

<PostBuildEvent>

IF "$(Platform)" == "x86" (
xcopy /s /y "$(ProjectDir)\nativedll\somelib\x86" "$(TargetDir)"
)
IF "$(Platform)" == "x64" (
xcopy /s /y "$(ProjectDir)\nativedll\somelib\x64" "$(TargetDir)"
)
</PostBuildEvent>

但是 dll 被复制到项目的 bin 文件夹中,而不是引用它的项目的 bin 文件夹中。

所以我现在的解决方案是在所有使用这个脚本的项目中添加一个后期构建脚本。

在 Visual Studio 中是否有更好的方法来执行此操作?

最佳答案

尝试对每个必须复制的文件使用它(csproj 文件 - 制作一个项目组):

<ItemGroup>
<ContentWithTargetPath Include="mySourcePath\myFile.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<TargetPath>myFile.dll</TargetPath>
</ContentWithTargetPath >
</ItemGroup>

引用项目还应包含复制的文件。


以下内容也可能有所帮助:

Copy native dependencies locally in a Visual Studio project

Add native files from NuGet package to project output directory @kjbartel

关于c# - Visual Studio 中自动复制 native dll 到引用项目的 bin 文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46361127/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com