简体   繁体   English

自动将本机dll复制到Visual Studio中引用项目的bin文件夹中

[英]Automaticaly copy native dll to the bin folder of referencing project in Visual Studio

I have some native dll which must be copied in the bin folder with platform conditions. 我有一些本机dll,必须根据平台条件将其复制到bin文件夹中。 And I want them to be copied to the bin folder of projects referencing this project. 我希望将它们复制到引用该项目的项目的bin文件夹中。

If I set the build action to Content , they will be copied to the bin folder but with the folder structure kept so they will not be in the bin folder but in sub folders. 如果将构建操作设置为Content ,它们将被复制到bin文件夹中,但文件夹结构保持不变,因此它们不会位于bin文件夹中,而是位于子文件夹中。 So when running the program, it will not be able to resolve the dll because they are in a subfolder. 因此,在运行程序时,它将无法解析dll,因为它们位于子文件夹中。

For example if I have this code in the project file 例如,如果我在项目文件中有此代码

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

The dll will be in the folder bin\\nativedll\\somelib\\x86\\somelib.dll in the case of x86. 对于bin\\nativedll\\somelib\\x86\\somelib.dll该dll将位于文件夹bin\\nativedll\\somelib\\x86\\somelib.dll中。

So I tried to use Post build script 所以我尝试使用Post构建脚本

<PostBuildEvent>

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

But the dll are copied in the bin folder of the project but not in the bin folder of projects referencing it. 但是dll将复制到项目的bin文件夹中,但不会复制到引用它的项目的bin文件夹中。

So my solution right now is to add a post build script in all projects using this one. 因此,我现在的解决方案是使用此脚本在所有项目中添加一个后构建脚本。

Is there a better way to do this in Visual Studio? 在Visual Studio中有更好的方法吗?

Try using this for each file that has to be copied (csproj file - make an item group): 尝试对每个必须复制的文件使用此文件(csproj文件-创建项目组):

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

The referencing project should also contain the copied files. 引用项目还应包含复制的文件。


The following may help as well: 以下内容也可能会有所帮助:

Copy native dependencies locally in a Visual Studio project 在Visual Studio项目中本地复制本机依赖项

Add native files from NuGet package to project output directory @kjbartel 将NuGet包中的本机文件添加到项目输出目录@kjbartel

I give the solution I used before I got @dajuric anwser. 我给出了@dajuric anwser之前使用的解决方案。 I prefer dajuric solution because it doesn't involve code looking in an other folder. 我更喜欢dajuric解决方案,因为它不涉及在其他文件夹中查找代码。

I used conditional Content in the csproj. 我在csproj中使用了条件内容。

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

Then before initialising the library using the native dll, I added the folder to the dll lookup folder list with kernel32 SetDllDirectory . 然后,在使用本机dll初始化库之前,我使用kernel32 SetDllDirectory将文件夹添加到dll查找文件夹列表中。

        [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool SetDllDirectory(string lpPathName);

 SetDllDirectory(@".\nativedll\somelib\x"+ (Environment.Is64BitProcess ? "64" : "32"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Visual Studio构建解决方案不会将dll复制到bin文件夹,但是构建项目可以 - Visual Studio building solution does not copy dll to bin folder, but building project does Visual Studio / MSBuild将引用类库的app.config复制为* .dll.config到当前项目的bin文件夹 - Visual Studio/MSBuild copy referenced class library's app.config as *.dll.config to bin folder of current project 将projectname.dll文件从我的Visual Studio项目复制到IIS bin文件夹中是正确的 - Copying the projectname.dll file from my visual studio project to my IIS bin folder is this correct 在Visual Studio 2008中引用dll二进制文件或引用项目时有区别吗? - Is there a difference when referencing dll binaries or referencing a project in Visual Studio 2008? Visual Studio 2013-构建后事件-按主项目bin文件夹复制 - Visual Studio 2013- Post-build event - copy by main project bin folder Visual Studio 将.dll 复制到发布时的 Bin 文件夹 - Visual Studio copies .dll's to Bin folder on Publish Visual Studio 2022 将引用的 dll 放入 bin 文件夹的子目录中 - Visual Studio 2022 place referenced dll into subdirectory of bin folder 发布dll - 复制到bin文件夹 - Publish dll - copy to bin folder Visual Studio C#.dll与项目引用 - Visual Studio C# .dll vs. project referencing 如何在bin / Release for Visual Studio Project中创建文件夹? - How to create a folder in bin/Release for Visual Studio Project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM