简体   繁体   English

将第三方程序集复制到子文件夹

[英]Copy third party assemblies to sub folder

I use various 3rd party assemblies in my project (native, nuget packages) that are sometimes optimized for different platforms (x86/amd64). 我在项目中使用了各种第三方程序集(本机,nuget程序包),这些程序集有时针对不同的平台(x86 / amd64)进行了优化。 Visual Studio automatically copies them in the application root (usually bin\\Debug\\PLATFORM), which creates a completely unstructorized mess. Visual Studio会自动将它们复制到应用程序根目录(通常为bin \\ Debug \\ PLATFORM)中,这会创建一个完全未经结构化的混乱。

Now I'm looking for a way to automatically copy 3rd party dlls into a custom subdirectory. 现在,我正在寻找一种将第三方dll自动复制到自定义子目录中的方法。 I'm aware of various ways to LOAD assemblies from a different location than the application root (privatePath in app.config or in code), but not how to automatically copy them there. 我知道有多种方法可以从不同于应用程序根目录(app.config或代码中的privateatePath)的位置加载程序集,但是不知道如何自动将其复制到那里。

Desired structure 所需结构

bin\
    release\
        x64\
            MyApp.exe
            MyApp.exe.config
            cfg\
                custom.config
            lib\
                ninject\
                    Ninject.dll
                    Ninject.Extensions
                    Ninject.Extensions.Logging
                SomeNativeDll\
                    native_x64.dll
                OtherStuff.dll
        x86\
            ...

I've already found a way with Post Build Events, but declaring every reference with some batch like script language is a PITA! 我已经找到了构建后事件的方法,但是用脚本语言(如脚本语言)声明每个引用都是PITA!

One possible solution is to add one extra project with your structure. 一种可能的解决方案是在您的结构中添加一个额外的项目。 The following steps recreate that: 以下步骤重新创建了该步骤:

  1. Add a dll project:placeholder.csproj 添加一个DLL项目:placeholder.csproj
  2. Create the folder 'lib' 创建文件夹“ lib”
  3. Create the subfolder 'ninject' 创建子文件夹“ ninject”
  4. Add the file 'Ninject.dll' to that folder 将文件“ Ninject.dll”添加到该文件夹
    1. set the BuildAction to 'Content' 将BuildAction设置为“ Content”
  5. Add the placholder.csproj as a reference to yourt main project 添加placholder.csproj作为对您的主项目的引用
  6. Build 建立

The Content files (but really being your dll's) are now copied in the same folder structure in the output folder of your main project. 现在, 内容文件(但实际上是您的dll)被复制到主项目的输出文件夹中的相同文件夹结构中。

To support the platform specfic dll's it becomes a little trickier. 为了支持平台特定的dll,它变得有些棘手。

Unload the project and find your native file: 卸载项目并找到您的本机文件:

<Content Include="test\Some_X86.dll" >
 <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>

and add a condition to it 并为其添加条件

<Content Include="test\Some_X86.dll" Condition="'$(Platform)' == 'x86'" >
  <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>

if you only want to have the files for the x86 target in the output. 如果只想在输出中包含x86目标的文件。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM