Reputation: 3097
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.
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...
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.
How do I do this?
First thing I tried was to add the following to the .csproj
file
<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.
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
But I couldn't get any files to copy...
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>
I don't see any files when I build the project where this package is installed.
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?
Upvotes: 1
Views: 2636
Reputation: 3097
I solved this by updating my .csproj
to the following for each required file...
<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 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...
Which means the code to find the file folder based on the currently executing assembly works as hoped.
Upvotes: 3