Copying files from an inherited nuget - c#

I've developed a custom NuGet which uses DotNetProjects.DotNetSiemensPLCToolBoxLibrary. DotNet library includes external dlls that are copied into the output directory following these statements:
<Content Include="..\externalDlls\libnodave_jfkmod.dll">
<Pack>true</Pack>
<PackageCopyToOutput>true</PackageCopyToOutput>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="..\externalDlls\libnodave_jfkmod64.dll">
<Pack>true</Pack>
<PackageCopyToOutput>true</PackageCopyToOutput>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
However, when my custom Nuget is used in another project, DotNet dlls are shown on project tree but not copied into the output directory.
nuget use example
If I set manually that these dlls will always be copied into output directory it works. It is posible to do so in the .csproj of my custom nuget?.

You can modify the .nuspec file instead of csproj file.
<files>
<!-- the dlls are copied from src to target-->
<file src="lib\**" target="lib/{framework name}[{version}]" />
</files>
The link: Support multiple .NET versions

Related

Nuget pack and install third party dependencies, not located in the bin folder

I am trying to create a NuGet package that can contain and deploy third party dependencies, these are dll's and executables that my program needs to run. There are a few caveats to this issues though.
When installing the package the dlls need to be in one directory above the bin folder, I can already make them install into the bin folder thats not what I need. (This makes this question different than others I have found)
I would prefer using the packing via the .csproj file instead of the .nuspec if possible.
If I can't place them one level up I could probably make this work installing them into a folder within the bin folder. These requirements are a little weird but I'm a bound by work that has already been done and don't have power to change it.
A small example of one of the many things I have tried:
<Content Include="x86\SQLite.Interop.dll">
<buildAction>Embedded Resource</buildAction>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<copyToOutput>true</copyToOutput>
<PackagePath>lib</PackagePath>
<PackageOutputPath>..\SQLite</PackageOutputPath>
<Pack>true</Pack>
</Content>
Use this:
<ItemGroup>
<Content Include="x86\SQLite.Interop.dll">
<buildAction>Embedded Resource</buildAction>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<copyToOutput>true</copyToOutput>
<PackagePath>lib\$(targetframework)</PackagePath>
<Pack>true</Pack>
</Content>
</ItemGroup>
<PropertyGroup>
<PackageOutputPath>..\SQLite</PackageOutputPath>
</PropertyGroup>
Then, re-pack your project. Before you install this new version, please delete all old nuget caches under C:\Users\xxx\.nuget\packages, then reinstall the new version. After that, re-build your main project and then you will get what you want.
Update
Try to pack the files into content node:
<ItemGroup>
<Content Include="x86\SQLite.Interop.dll">
<buildAction>Embedded Resource</buildAction>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<PackageCopyToOutput>true</PackageCopyToOutput>
</Content>
</ItemGroup>
Then, right-click on the Project-->Pack. And you should not use any other nuspec file to pack with.

Add SQLite dtabase file from a class library to ASP.NET Core MVC output directory on project build

I created a class library project and put my SQLite database in it, and set these lines in the .csproj file:
<ItemGroup>
<Content Include="Data\DataBase\locations.db">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
If I locally reference that class library to my main project (ASP.NET Core), it works fine and on every build it will copy the database file to this directory:
***\bin\Debug\net5.0\Data\DataBase
But when I publish the class library to NuGet, and install it in my main project, it doesn't copy the file.
Any help why this is happening and what should I do?
This is my package in NuGet package explorer
Just find out the answer
<ItemGroup>
<Content Include="Data\DataBase\locations.db">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<PackageCopyToOutput>true</PackageCopyToOutput>
</Content>
</ItemGroup>
This line will do the job
<PackageCopyToOutput>true</PackageCopyToOutput>

Including a Folder in NuGet Package and have it install into project as file .netcore/Razor

I am using Visual Studio 2019 and creating NuGet packages successfully with this method:
https://learn.microsoft.com/en-us/nuget/quickstart/create-and-publish-a-package-using-visual-studio?tabs=netcore-cli
All going well, but there are some settings (.json) files contained within a directory PageSettings/
When I publish my NuGet package and then install it into a new project, this directory appears in VS as a linked item (see pic). So as a user I can access the files, but they don't "exist" when the project is run.
This means if I run this project without physically copying and adding these files I get ArgumentException: The directory name 'Path-To-project\pagesettings' does not exist. (Parameter 'Path')
I can see why this is happening, but can't work out how to change it, or if it is possible.
The article linked above suggests adding code to the csproj file like:
<ItemGroup>
<Content Include="readme.txt">
<Pack>true</Pack>
<PackagePath>\</PackagePath>
</Content>
</ItemGroup>
But that doesn't work and in fact seems unnecessary since the Pack command is including my files, just not creating them properly when installing.
Also - it would be extremely handy if there was a way to tell VS to prompt whether to install this file or not. Since it is settings, if a user changes the settings and then later installs an updated version of my NuGet package I would not want it to overwrite their customised settings... perhaps this is why the link design happens... if so, if someone could confirm?
Actually, you should create a .props file in your nuget package.
1) create a file called <package_id>.props file in your nuget project.
Like this:
Note: if your created nuget package called test.1.0.0.nupkg, the file should be named as test.props so that it will work.
2) add these in the test.props file:
<Project>
<Target Name="CopyFiles" BeforeTargets="Build">
<ItemGroup>
<File Include="$(MSBuildThisFileDirectory)..\Pagesettings\*.*"></File>
</ItemGroup>
<Copy SourceFiles="#(File)" DestinationFolder="$(ProjectDir)Pagesettings"></Copy>
</Target>
</Project>
3) add these in xxx.csproj file:
<ItemGroup>
<None Include="Pagesettings\*.*(the json files' path of your nuget project)" Pack="true" PackagePath="Pagesettings">
</None>
<None Include="build\*.*" Pack="true" PackagePath="build"></None>
</ItemGroup>
then reapck your project.
4) then clean your nuget caches or delete all files under C:\Users\xxx(current user)\.nuget\packages.
5) when you insall this new version of the nuget package, please build your main project again to run the target to generate the files.
Besides, there is also a similar issue about this.

Nuget package to copy exe content file to project output directory

I'm attempting to create a Nuget package that will copy an executable file to the output directory of a .Net framework library.
Here is my nuspec file:
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
<metadata>
<id>CopyExeToOutputNugetPackage</id>
<version>1.0.0</version>
<authors>Some Dude</authors>
<owners>Some Owner</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>A package to copy an exe to the output directory.</description>
<tags>CopyExeToOuput</tags>
<contentFiles>
<files include=".\content\test.exe" buildAction="None" copyToOutput="true" />
</contentFiles>
</metadata>
</package>
The "nuget pack" command works fine and builds my .nupkg file. I can then add the nuget project to my .Net Framework project and the test.exe file is added to my project:
<ItemGroup>
<Content Include="test.exe" />
</ItemGroup>
I can then use Visual Studio to edit the file properties to copy to the output directory and my project file is updated:
<ItemGroup>
<Content Include="test.exe">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
I would prefer that this last manual step is no required.
I've attempted to use a .targets file but that's either the wrong path or I never got the configuration correct.
I've also tried using the nuspec files element (instead of ):
<files>
<file src="test.exe" target="lib\net462" />
</files>
With this last configuration, I get the following exception when attempting to add the nuget package to my .Net Framework v4.6.2 project:
Failed to add reference to 'test'. Please make sure that the file is accessible, and that it is a valid assembly or COM component.
I got a working answer that uses a PowerShell script and PowerShell Tools for Visual Studio on a Microsoft forum:
https://social.msdn.microsoft.com/Forums/en-US/cb6236e8-4705-485b-a47c-cc4dc933c92c/nuget-package-to-copy-exe-content-file-to-project-output-directory?forum=visualstudiogeneral

VS2010 warning while creating link to a file

In my C# project I have some .docx,.xlsx,*.jpg files in an folder, I want to make all these files as Content and while Setup and Deployment Project I want to add all these files as Content Output for That I have done follwing steps:
Unload Project from property window and then selected to Edit .csproj file.
Added below lines in <ItemGroup> to make all files in folder as Content and linked them to a folder
<Content Include="Lib\MyApp\Support\**\*.*">
<Link>Support\%(RecursiveDir)%(FileName).%(Extension)</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Lib\MyApp\Files\**\*.*">
<Link>Files\%(RecursiveDir)%(FileName).%(Extension)</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Reload Project
After reloading project I got lots of warning like
Warning 34 The file 'Lib\MyApp\Files\abc.bmp' could not be added to
the project. Cannot add a link to the file
D:\MyApp\Lib\MyApp\Files\abc.bmp. This file is within the project
directory tree.
I have Excluded the Lib folder from project, Why I am getting such warnings what I am doing wrong? Is it safe to ignore such warnings.
Update
After modifying csproj file to this
<Content Include="Lib\QOES\Support\**\*.*">
<Link>Support\%(RecursiveDir)%(FileName)%(Extension)</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Visible>false</Visible>
</Content>
<Content Include="Lib\QOES\Files\**\*.*">
<Link>Files\%(RecursiveDir)%(FileName)%(Extension)</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Visible>false</Visible>
</Content>
after adding <Visible>false</Visible> removed warnings, but is this correct way?
Adding <Visible>false</Visible> seems to be a very convenient solution to get rid of the Visual Studio warning:
The file <filename> could not be added to the project. This file is
within the project directory tree.
According to: MSDN: The file 'file' could not be added to the project, one of the reasons is relative paths being ambiguous. Meaning you have the file path Lib/MyApp/Files/abc.bmp both as a file path in your project folder as well as on your D: drive leading to ambiguity. That is my guess. Adding files by hand to the project folder can lead to issues.
In my case where I wanted to place a native binary aside of the executable, using <TargetPath> instead of <Link> worked:
<Content Include="Lib\somelibrary.dll">
<TargetPath>somelibrary.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>

Categories