在 NuGet 包中包含一个文件夹,并将其作为文件安装到项目中。netcore/Razor

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

我正在使用 Visual Studio 2019 并使用此方法成功创建 NuGet 包:

https://docs.microsoft.com/en-us/nuget/quickstart/create-and-publish-a-package-using-visual-studio?tabs=netcore-cli

一切顺利,但目录 PageSettings/

中包含一些设置 (.json) 文件

当我发布我的 NuGet 包并将其安装到新项目中时,此目录在 VS 中显示为 linked item(见图)。因此,作为用户,我可以访问这些文件,但当项目为 运行.

时,它们并不“存在”

这意味着如果我 运行 这个项目没有物理复制和添加这些文件我得到 ArgumentException: The directory name 'Path-To-project\pagesettings' does not exist. (Parameter 'Path')

我明白为什么会这样,但不知道如何改变它,或者是否可能。

上面的 link 文章建议将代码添加到 csproj 文件,例如:

<ItemGroup>
  <Content Include="readme.txt">
    <Pack>true</Pack>
    <PackagePath>\</PackagePath>
  </Content>
</ItemGroup>

但这不起作用,事实上似乎没有必要,因为 Pack 命令 包括我的文件,只是在安装时没有正确创建它们。

此外-如果有一种方法可以告诉VS提示是否安装此文件,那将非常方便。因为它是设置,如果用户更改设置然后安装我的 NuGet 包的更新版本我不希望它覆盖他们的自定义设置......也许这就是 link 设计发生的原因......如果是这样,是否有人可以确认?

实际上,您应该在 nuget 包中创建一个 .props 文件。

1) 在您的 nuget 项目中创建一个名为 .props 的文件。

像这样:

注意:如果您创建的 nuget 包名为 test.1.0.0.nupkg,该文件应命名为 test.props 以便它可以工作。

2)test.props 文件中添加这些:

<Project>
  <Target Name="CopyFiles" BeforeTargets="Build">
    <ItemGroup>     
      <File Include="$(MSBuildThisFileDirectory)..\Pagesettings\*.*"></File>          
    </ItemGroup>
      <Copy SourceFiles="@(File)" DestinationFolder="$(ProjectDir)Pagesettings"></Copy>
  </Target>
</Project>

3)xxx.csproj 文件中添加:

   <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>

然后重新打包您的项目。

4) 然后 clean your nuget caches 或删除 C:\Users\xxx(current user)\.nuget\packages.

下的所有文件

5) 当你安装这个新版本的 nuget 包时,请再次 build 你的主项目到 运行生成文件的目标。

除了,还有.