0

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.

enter image description here

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?

Jamie Hartnoll
  • 7,231
  • 13
  • 58
  • 97
  • How does your `csproj` file look like? You probably should specify the `PackagePath` property, as well as use `IncludeInPackage="true"` – Pavel Anikhouski Oct 01 '20 at 20:02
  • You may also write your own msbuild `.props` or `.targets` file to correctly add required files to project, where package is installed – Pavel Anikhouski Oct 01 '20 at 20:06
  • Hi,Jamie, any update about this issue? Please check if my answer helps you handle the issue, if it helps, please do not forget to accept it. And if not, please feel free to let us know:) – Mr Qian Oct 05 '20 at 01:52
  • Sorry - have not had chance to try this yet. I will try to this afternoon! – Jamie Hartnoll Oct 05 '20 at 13:20

1 Answers1

0

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:

enter image description here

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.

Mr Qian
  • 21,064
  • 1
  • 31
  • 41
  • This works! Excellent, thank you. The only trouble now is that these settings files are user-editable, if a user downloads an update to my NuGet package it will overwrite their user edited versions of the `.json` files. Any idea how I can get VS to prompt the user on Build to back up their settings and/or give an option to overwrite or ignore these files? – Jamie Hartnoll Oct 05 '20 at 17:27
  • Still not working, it's still a link. – Harrison Wu Mar 03 '23 at 09:25