简体   繁体   English

Nuget Library.exe 未出现在主项目中

[英]Nuget Library .exe's are not Appearing In Main Project

I have been at this for over a week now and I am still having no luck.我已经在这里待了一个多星期了,但我仍然没有运气。

Here is what I am trying to achieve in short -简而言之,这是我要实现的目标-

I have a Nuget library that contains two.exe files that only the library uses.我有一个 Nuget 库,其中包含两个只有该库使用的 .exe 文件。 The problem is, that when the library is pulled into the main project, the exes are either not being put in the output folder properly, or the path to them is turning into the main project path instead of the nuget/lib path.问题是,当库被拉入主项目时,exes 要么没有正确地放在 output 文件夹中,要么它们的路径变成了主项目路径而不是 nuget/lib 路径。

The Library图书馆

Here is the.csproj这是.csproj

  <ItemGroup>
      <Content Include="Drivers/**">
          <Pack>true</Pack>
          <PackagePath>lib\net5.0\Drivers\</PackagePath>
          <CopyToOutputDirectory>Always</CopyToOutputDirectory>
          <CopyToPublishDirectory>Always</CopyToPublishDirectory>
          <PackageCopyToOutput>Always</PackageCopyToOutput>
      </Content>
  </ItemGroup>

How I am using the exe's in the library's code -我如何在库的代码中使用 exe -

var path = $"{AppDomain.CurrentDomain.BaseDirectory}Drivers";

This successfully packs up my project and allows me to use the exe's when I run the unit tests on our packaging pipelines.这成功打包了我的项目,并允许我在打包管道上运行单元测试时使用 exe。 When downloaded into the new project, it creates a nuget directory with the path下载到新项目时,它会创建一个 nuget 目录,路径为

{USER}\.nuget\packages\drivers.library\1.0.X

In that directory there are three folders在该目录中有三个文件夹

--content (contains testhost stuff)
--contentFiles (contains testhost stuff)
--lib
   \ net5.0
         \ Drivers
             \ exe1 and exe2  

The Main Project主要项目

So after importing the library所以导入库后

 <PackageReference Include="Drivers.Framework" Version="1.0.8" />

I would of expected the "Drivers" folder to be built into the OutputDirectory of the main project based on the settings that were in the library's.csproj file.我希望根据库的 .csproj 文件中的设置将“Drivers”文件夹内置到主项目的OutputDirectory中。

I would of also expected that using AppDomain.CurrentDomain.BaseDirectory in the library would of defaulted to the location where the.dll is located in the.nuget directory.我还希望在库中使用AppDomain.CurrentDomain.BaseDirectory将默认为 .dll 位于 .nuget 目录中的位置。

However, that's obviously not the case.然而,事实显然并非如此。

As far as trying to get the.dll location, I've tried -至于试图获得 .dll 位置,我试过 -

        "1" + Assembly.GetExecutingAssembly().Location; 
        "2" + Assembly.GetExecutingAssembly().CodeBase; 
        "3" + AppDomain.CurrentDomain.BaseDirectory;
        "4" + AppDomain.CurrentDomain.DynamicDirectory;
        "5" + AppDomain.CurrentDomain.FriendlyName;
        "6" + AppDomain.CurrentDomain.RelativeSearchPath;

but it all points to where the main project is and not the library.但这都指向主要项目所在的位置而不是库。

As far as getting the Drivers folder from the.nuget directory to the main project's output folder.至于将 Drivers 文件夹从 .nuget 目录移至主项目的 output 文件夹。 I've tried some recursion in the csproj, but I was only able to grab the dependency.dlls and not the exes.我在 csproj 中尝试了一些递归,但我只能获取 dependency.dll 而不是 exe。 I have also seen some people use powershell, but I would like to absolutely avoid that if possible.我也看到有人使用 powershell,但我想尽可能避免使用。

If anyone has any idea of how to get the nuget/lib/Drivers folder to build into the Main project's output folder, I would be forever grateful.如果有人知道如何将nuget/lib/Drivers文件夹构建到主项目的 output 文件夹中,我将永远感激不已。

Thank you guys.感谢你们。

So I figured out where I was going wrong -所以我想出了我哪里出错了 -

In the library's csproj file it should simply read -在库的 csproj 文件中,它应该简单地读取 -

  <ItemGroup>
      <Content Include="Drivers/**">
          <CopyToPublishDirectory>Always</CopyToPublishDirectory>
          <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      </Content>
  </ItemGroup>

notice that I took out the line请注意,我取出了线

<PackagePath>lib\net5.0\Drivers\</PackagePath>

which was inhibiting it from building to the content/contentFiles folders of the local .nuget/packages/this.library path.这阻止它构建到本地.nuget/packages/this.library路径的content/contentFiles文件夹。

Once I took out that line, the "Drivers" folder and the exe's in that folder built into the main project as expected.删除该行后,“Drivers”文件夹和该文件夹中的 exe 将按预期内置到主项目中。

From there, I added this to the main project's csproj.从那里,我将其添加到主项目的 csproj 中。

<Target Name="CopyLinkedContentFiles" BeforeTargets="Build">
    <Copy SourceFiles="%(Content.Identity)"
          DestinationFiles="$(OutputPath)\%(Content.Link)"
          SkipUnchangedFiles="true"
          OverwriteReadOnlyFiles="true" />
</Target>

This copies the "Linked" files (in this case the Drivers folder), to the OutputFolder of the main project.这会将“链接”文件(在本例中为 Drivers 文件夹)复制到主项目的 OutputFolder。

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

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