簡體   English   中英

如何將其他文件添加到 nuget package 並在 nuget ZEFE90A8E604A7C67D803A 代碼中引用這些文件?

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

我想在nuget package 中提供一些其他文件,以便無論何時何地安裝它,都可以在正確的位置提供文件。

我遇到的問題是我的代碼需要引用這些文件才能運行。

這些文件是單個目錄中的 3 個.exe文件和一個.jar文件...

在此處輸入圖像描述

這是引用該文件夾位置的代碼...

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

我希望能夠 package 此代碼和文件向上,以便每當安裝此 nuget package 時,此代碼將指向有效位置。

我該怎么做呢?

我嘗試的第一件事是將以下內容添加到.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>

僅當您在同一解決方案中引用該項目時,這才有效。 在您完成dotnet pack並在不同的項目/解決方案中安裝 package 后,它不起作用。

我嘗試的第二件事是這里接受的答案......

將文件從 Nuget package 復制到 output 目錄中,並使用 Z4AE297DCDE37684313907BD7A3 中的 Z4AE297DCDE3768438B61.cspro7A3 命令

但是我無法復制任何文件...

在此處輸入圖像描述

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

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

當我構建安裝了這個 package 的項目時,我沒有看到任何文件。

即使這確實有效,我如何確保 package 中的代碼在從任何項目運行時始終指向這些文件?

我通過將每個所需文件的.csproj更新為以下內容來解決這個問題......

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

dotnet pack之后,我能夠通過檢查 package 查看文件

在此處輸入圖像描述

將 nuget package 添加到新項目並構建解決方案后,我能夠看到bin文件夾中的文件...

在此處輸入圖像描述

這意味着根據當前執行的程序集查找文件夾的代碼可以按預期工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM