[英]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 后,它不起作用。
我嘗試的第二件事是這里接受的答案......
但是我無法復制任何文件...
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.