问题描述
我正在使用 Visual Studio 2019 并使用此方法成功创建 NuGet 包:
这意味着如果我在没有物理复制和添加这些文件的情况下运行这个项目,我会得到 ArgumentException: The directory name 'Path-To-projectpagesettings' 不存在.(参数'路径')
我知道为什么会发生这种情况,但不知道如何改变它,或者是否可能.
上面链接的文章建议将代码添加到 csproj
文件中,例如:
<内容包括=readme.txt"><包>真的</包><PackagePath></PackagePath></内容></项目组>
但这不起作用,实际上似乎没有必要,因为打包命令是包括我的文件,只是在安装时没有正确创建它们.
另外 - 如果有办法告诉 VS 提示是否安装此文件,那将非常方便.因为它是设置,如果用户更改设置然后安装我的 NuGet 包的更新版本,我不希望它覆盖他们的自定义设置......也许这就是链接设计发生的原因......如果是这样,如果有人可以确认吗?
其实,你应该在你的nuget包中创建一个.props
文件.
1) 在您的 nuget 项目中创建一个名为 <package_id>.props 文件的文件.
像这样:
注意:如果您创建的 nuget 包名为 test.1.0.0.nupkg
,则该文件应命名为 test.props
所以它会起作用.
2) 将这些添加到 test.props
文件中:
<项目><目标名称=CopyFiles"BeforeTargets="构建"><项目组><文件包含="$(MSBuildThisFileDirectory)..Pagesettings*.*></File></项目组><复制源文件=@(文件)"DestinationFolder="$(ProjectDir)Pagesettings"></Copy></目标></项目>
3) 将这些添加到 xxx.csproj
文件中:
<ItemGroup><None Include="Pagesettings*.*(你的 nuget 项目的 json 文件路径)"包=真";PackagePath="页面设置"></无><无包含=build*.*"包=真";PackagePath="build"></None></项目组>
然后重新打包您的项目.
4) 然后 清理你的 nuget 缓存 或删除 C:Usersxxx(current user).nugetpackages
下的所有文件.
5)当您安装此新版本的 nuget 包时,请再次构建您的主项目以运行目标以生成文件.
除了,还有类似的问题.
I am using Visual Studio 2019 and creating NuGet packages successfully with this method:
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-projectpagesettings' 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:Usersxxx(current user).nugetpackages
.
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 包中包含一个文件夹并将其作为文件 .netcore/Razor 安装到项目中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!