简体   繁体   English

如何将其他文件添加到 nuget package 并在 nuget ZEFE90A8E604A7C67D803A 代码中引用这些文件?

[英]How to add additional files to a nuget package and reference those files within the nuget package code?

I'm wanting to provide some additional files in a nuget package so that whenever and wherever it is installed, the files are provided and in the correct location.我想在nuget package 中提供一些其他文件,以便无论何时何地安装它,都可以在正确的位置提供文件。

The problem I'm having is that I have code that needs to reference these files in order to run.我遇到的问题是我的代码需要引用这些文件才能运行。

The files are 3 .exe files and a .jar file in a single directory...这些文件是单个目录中的 3 个.exe文件和一个.jar文件...

在此处输入图像描述

This is the code that references that folder location...这是引用该文件夹位置的代码...

var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\WebDrivers\\Drivers\\";

I want to be able to package this code and the files up so that whenever this nuget package is installed, this code will point to a valid location.我希望能够 package 此代码和文件向上,以便每当安装此 nuget package 时,此代码将指向有效位置。

How do I do this?我该怎么做呢?

First thing I tried was to add the following to the .csproj file我尝试的第一件事是将以下内容添加到.csproj文件中

<ItemGroup>
  <None Update="WebDrivers\Drivers\chromedriver.exe">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </None>
  <None Update="WebDrivers\Drivers\geckodriver.exe">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </None>
  <None Update="WebDrivers\Drivers\msedgedriver.exe">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </None>
  <None Update="WebDrivers\Drivers\selenium-server-standalone-3.141.0.jar">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </None>
</ItemGroup>

This only works if you reference the project in the same solution.仅当您在同一解决方案中引用该项目时,这才有效。 It doesn't work after you have done a dotnet pack and installed the package in a different project/solution.在您完成dotnet pack并在不同的项目/解决方案中安装 package 后,它不起作用。

Second thing I tried was the accepted answer here...我尝试的第二件事是这里接受的答案......

Copy files from Nuget package to output directory with MsBuild in.csproj and dotnet pack command 将文件从 Nuget package 复制到 output 目录中,并使用 Z4AE297DCDE37684313907BD7A3 中的 Z4AE297DCDE3768438B61.cspro7A3 命令

But I couldn't get any files to copy...但是我无法复制任何文件...

在此处输入图像描述

SASelenium.Framework.csproj SASelenium.Framework.csproj

  <ItemGroup Label="FilesToCopy">
    <Content Include="SASelenium.Framework.targets" PackagePath="build/SASelenium.Framework.targets" />
    <Content Include="LogFiles\*.config" Pack="true" PackagePath="contentFiles\LogFiles">
      <PackageCopyToOutput>true</PackageCopyToOutput>
    </Content>
  </ItemGroup>

SASelenium.Framework.targets SASelenium.Framework.targets

<ItemGroup>
  <LogFiles Include="$(MSBuildThisFileDirectory)\..\contentFiles\LogFiles\*.config" />
</ItemGroup>
<Target Name="CopyLogFiles" BeforeTargets="Build">
  <Copy SourceFiles="@(LogFiles)" DestinationFolder="$(TargetDir)CopiedLogFiles\" />
</Target>

I don't see any files when I build the project where this package is installed.当我构建安装了这个 package 的项目时,我没有看到任何文件。

Even if this did work, how would I ensure the code within the package is always pointing at these files when running from any project?即使这确实有效,我如何确保 package 中的代码在从任何项目运行时始终指向这些文件?

I solved this by updating my .csproj to the following for each required file...我通过将每个所需文件的.csproj更新为以下内容来解决这个问题......

<Content Include="WebDrivers\Drivers\chromedriver.exe">
  <CopyToOutputDirectory>Always</CopyToOutputDirectory>
  <PackageCopyToOutput>true</PackageCopyToOutput>
  <pack>true</pack>
</Content>

After a dotnet pack I was able to see the files by inspecting packagedotnet pack之后,我能够通过检查 package 查看文件

在此处输入图像描述

After adding the nuget package to a new project and building the solution I was able to see the files in the bin folder...将 nuget package 添加到新项目并构建解决方案后,我能够看到bin文件夹中的文件...

在此处输入图像描述

Which means the code to find the file folder based on the currently executing assembly works as hoped.这意味着根据当前执行的程序集查找文件夹的代码可以按预期工作。

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

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