有没有更好的方法将本机dll复制到bin文件夹?

Ahm*_*mad 17 .net build post-build-event visual-studio

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

我尝试了以下方法

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

    copy "$(ProjectDir)Libs\NQuantLibc.dll" "$(TargetDir)NQuantLibc.dll"

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

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

以上两个选项都有效.有没有更好的方法将本机dll复制到bin文件夹,以便始终解析依赖项?或者,这种情况有不同的方法吗?

Han*_*ant 28

使用Project + Add Existing Item并选择DLL.在Solution Explorer窗口中选择添加的文件.在"属性"窗口中,将"复制到输出目录"设置更改为"如果更新则复制".

  • @Ahmad:在对话框中添加现有项目时,请注意"添加"按钮的下拉部分,并将其更改为"添加为链接".在UI中很容易错过. (4认同)
  • 我已经做了一个可能的选择 - 见第2点 (2认同)

Yoc*_*mer 15

您可以将本机dll添加为链接项,并使用" 如果更新则复制 ".
本机dll的问题在于,有时您会根据项目的配置(调试/发布或平台)使用不同的dll.

您可以编辑项目的.csproj并有条件地链接本机dll:

 <ItemGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|Win32' ">
    <Content Include="..\..\bin\Win32\Release\NQuantLibc.dll">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
 </ItemGroup>   
 <ItemGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|Win32' ">
    <Content Include="..\..\bin\Win32\Debug\NQuantLibc_d.dll">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
  </ItemGroup>
  <ItemGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' ">
    <Content Include="..\..\bin\x64\Debug\NQuantLibc_d.dll">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
  </ItemGroup>
  <ItemGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x64' ">
    <Content Include="..\..\bin\x64\Release\NQuantLibc.dll">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
  </ItemGroup>
Run Code Online (Sandbox Code Playgroud)

请注意,复制选项设置为PreserveNewest,这意味着"如果更新则复制".

  • 令人惊讶的是,过去的主线开发模式现在非常奇怪,您必须手动编辑.csproj文件. (4认同)

Flo*_*oyd 5

找到了更好的方法.Nuget可以将.targets文件,存储在包的build文件夹中,添加到您的项目中.通过这种方式,您可以在每个构建中复制包的任何文件,无论您想要什么.在下面的示例中,我将一些Non DotNet DLL存储到"binaries"文件夹中.在每次构建时,它会检查DLL是否已经复制到输出文件夹($ OutputPath变量)中,并在必要时复制它们.

Nuspec内容:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
    <metadata>
        <id>Example</id>
        <version>1.0.0</version>
        <authors>Example</authors>
        <requireLicenseAcceptance>false</requireLicenseAcceptance>
        <description>Example</description>
    </metadata>
    <files>
        <file src="Non-DotNet.dll" target="binaries\Non-DotNet.dll" />
        <file src="DotNet.dll" target="lib\net40\DotNet.dll" />
        <file src="Example.targets" target="build\Example.targets" />
    </files>
</package>
Run Code Online (Sandbox Code Playgroud)

Example.targets内容:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Target Name="CopyBinaries" BeforeTargets="BeforeBuild">
        <CreateItem Include="$(MSBuildThisFileDirectory)..\binaries\**\*.*">
            <Output TaskParameter="Include" ItemName="PackageBinaries" /> 
        </CreateItem>

        <Copy SourceFiles="@(PackageBinaries)"
              DestinationFolder="$(OutputPath)"
              SkipUnchangedFiles="true"
              OverwriteReadOnlyFiles="true"
        />
    </Target>
</Project>
Run Code Online (Sandbox Code Playgroud)