Nuget - 在子文件夹中的内容上设置CopyToOutputDirectory

Cra*_*ett 7 powershell nuget

我是Nuget的新手,我正在试图上传我的第一个包.到目前为止,一切都进展顺利.但是,我正在尝试在一些我想要存在于Lib子文件夹中的内容文件上设置CopyToOutputDirectory.我的目录如下所示:

?   Readme.txt
?   MyPackage.nupkg
?   MyPackage.nuspec
?
????content
?   ????Lib
?           native1.dll
?           native2.dll
?           native3.dll
?           native4.dll
?
????lib
?       MyActualAssembly.dll
?
????tools
        Install.ps1
Run Code Online (Sandbox Code Playgroud)

通过阅读这个StackOverflow问题和一些额外的阅读,我把一个看起来像这样的Install.ps1放在一起:

param($installPath, $toolsPath, $package, $project)

$project.ProjectItems.Item("Lib\native1.dll").Properties.Item("CopyToOutputDirectory").Value = 1
$project.ProjectItems.Item("Lib\native2.dll").Properties.Item("CopyToOutputDirectory").Value = 1
$project.ProjectItems.Item("Lib\native3.dll").Properties.Item("CopyToOutputDirectory").Value = 1
$project.ProjectItems.Item("Lib\native4.dll").Properties.Item("CopyToOutputDirectory").Value = 1
Run Code Online (Sandbox Code Playgroud)

我对各种操作进行了排列,看看它是否有助于我理解这个问题,但它实际上与答案相同.

从我的测试来看,Install.ps1在查找文件本身时遇到了一些麻烦.在安装包后运行时,我收到以下错误:

Exception calling "Item" with "1" argument(s): "The parameter is incorrect. (Exception from HRESULT: 0x80070057 
(E_INVALIDARG))"
At C:\...\tools\Install.ps1:3 char:1
+ $project.ProjectItems.Item("Lib\native1.dll").Properties.Item("CopyToOutputDirect ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation

Exception calling "Item" with "1" argument(s): "The parameter is incorrect. (Exception from HRESULT: 0x80070057 
(E_INVALIDARG))"
At C:\...\tools\Install.ps1:4 char:1
+ $project.ProjectItems.Item("Lib\native2.dll").Properties.Item("Copy ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation

Exception calling "Item" with "1" argument(s): "The parameter is incorrect. (Exception from HRESULT: 0x80070057 
(E_INVALIDARG))"
At C:\...\tools\Install.ps1:5 char:1
+ $project.ProjectItems.Item("Lib\native3.dll").Properties.Item("CopyToOutputDirec ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation

Exception calling "Item" with "1" argument(s): "The parameter is incorrect. (Exception from HRESULT: 0x80070057 
(E_INVALIDARG))"
At C:\...\tools\Install.ps1:6 char:1
+ $project.ProjectItems.Item("Lib\native4.dll").Properties.Item("CopyToOut ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation
Run Code Online (Sandbox Code Playgroud)

并且,正如您所期望的那样,所有文件都将其CopyToOutputDirectory设置设置为"不复制",默认设置.

我该如何解决这个问题?在ps脚本中访问子文件夹有不同的语法吗?或者我完全忽略了这些错误消息的意义?

Mat*_*ard 10

请尝试以下方法:

$project.ProjectItems.Item("Lib").ProjectItems.Item("native1.dll").Properties.Item("CopyToOutputDirectory").Value = 1
Run Code Online (Sandbox Code Playgroud)

我可能错了,但我不认为ProjectItems会允许您找到不是当前项目的直接子项的项目.因此,您需要首先找到Lib文件夹项目项,然后查看此项目项目中的dll.

为了测试这些,我通常打开Package Manager Console窗口,确保在Default project下拉列表中选择了正确的项目,然后使用命令行访问项目对象:

$ project = Get-Project

这为您提供了与NuGet安装脚本相同的功能,它是项目的Visual Studio对象模型.