我认为你对这一部分有一些误解。
First, 内容文件适用于 new-sdk 项目(Net Core
and Net Standard
) with PackageReference nuget管理格式 https://learn.microsoft.com/en-us/nuget/consume-packages/package-references-in-project-files而不是Net Framework
项目与packages.config nuget管理格式 https://learn.microsoft.com/en-us/nuget/reference/packages-config.
And contentFiles
适用于内容文件而不是 lib 文件夹。所以你不应该打包这些dll文件target="lib\any\any\.....dll"
。你应该将它们打包到contentFiles
folder.
用这个:
<contentFiles>
<files include="any/any/*" buildAction="Content" copyToOutput="true" flatten="true" />
</contentFiles>
<files>
<file src="xxx\First.dll(the physical, relative path of the dll on your project folder)" target="contentFiles\any\any\First.dll"></file>
<file src="xxx\Second.dll(the physical, relative path of the dll on your project folder)" target="contentFiles\any\any\Second.dll"></file>
<file src="xxx\....dll(the physical, relative path of the dll on your project folder)" target="contntFiles\any\any\.....dll"></file>
<files>
然后,您应该在Net Core项目。
完成后,重新打包项目nuget pack
命令,然后,在安装新命令之前,首先清理nuget缓存 https://learn.microsoft.com/en-us/nuget/consume-packages/managing-the-global-packages-and-cache-folders#clearing-local-folders删除旧的先前版本。然后,将新版本安装在Net Core项目中可以看到这样的效果:
=================================================== ===================
如果您仍想在Net Framework
项目,你应该将这些文件打包到content
节点而不是contentFiles
.
并且只需要添加两行:
<contentFiles>
<files include="any/any/*" buildAction="Content" copyToOutput="true" flatten="true" />
</contentFiles>
<files>
<file src="xxx\First.dll(the physical, relative path of the dll on your project folder)" target="contentFiles\any\any\First.dll"></file>
<file src="xxx\Second.dll(the physical, relative path of the dll on your project folder)" target="contentFiles\any\any\Second.dll"></file>
<file src="xxx\....dll(the physical, relative path of the dll on your project folder)" target="contntFiles\any\any\.....dll"></file>
<file src="xxx\First.dll(the physical, relative path of the dll on your project folder)" target="content"></file>
<file src="xxx\Second.dll(the physical, relative path of the dll on your project folder)" target="content"></file>
..........
<files>
But这些根本无法更改导入文件的属性。对于.net框架项目,无法更改文件的属性xxx.nuspec
file.
You应该使用.props 或目标 https://learn.microsoft.com/en-us/nuget/create-packages/creating-a-package#include-msbuild-props-and-targets-in-a-package file.
1)创建一个名为<packages_id>.props
在 build 文件夹下解决方案浏览器,如果您的 nuget 包名为ThisProject.1.0.0.nupkg
,你应该将其命名为ThisProject.props
这样它就会起作用。
这是我的:
2)将这些添加到 props 文件中:
<Project>
<ItemGroup>
<Content Include="First.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Second.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
......
</ItemGroup>
</Project>
3)添加一行nuspec
文件以将 props 文件包含到 nupkg.json 中。
<file src="build\xxx.props(the physical, relative path of the file on your project folder)" target="build"></file>
4)然后重新打包 nuget 包,清理 nuget 缓存,然后在 Net Framework 项目上安装这个新包packages.config
.
Note: 虽然特性解决方案资源管理器上导入的内容文件的窗口不显示更改的值,仍然显示旧值,文件已复制到项目的输出文件夹中。这是一个UI显示问题解决方案浏览器并且更改后的值已被使用并且运行良好。所以你不必太在意这一点。