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

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

来自阅读 this StackOverflow question和一些额外的阅读,我把一个 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

我对各种操作进行了 1 线排列,以查看它是否有助于我理解问题,但实际上与该答案相同。

根据我的测试,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

而且,正如您所期望的,所有文件的 CopyToOutputDirectory 设置都设置为默认设置为“不复制”。

我该如何解决?在 ps 脚本中访问子文件夹是否有不同的语法?还是我完全忽略了这些错误消息的要点?

最佳答案

请尝试以下操作:

$project.ProjectItems.Item("Lib").ProjectItems.Item("native1.dll").Properties.Item("CopyToOutputDirectory").Value = 1

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

为了测试这些,我通常打开包管理器控制台窗口,确保在默认项目下拉列表中选择了正确的项目,然后使用命令行访问项目对象:

$project = 获取项目

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

关于powershell - Nuget - 在子文件夹中的内容上设置 CopyToOutputDirectory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15460869/

相关文章:

powershell - 在 Powershell 中将对象添加到容器

arrays - powershell 中的字节类型转换

asp.net - System.Net.Http 引用属性路径不显示 NuGet 包路径

visual-studio - VSTS 包提要没有从上游源 (nuget.org) 拉包

powershell - Nuget 恢复命令行响应在源上找不到包 - Artifactory 云服务器

powershell - 使用 New-Object -Property 设置嵌套类的属性

powershell - PS 流水线多参数绑定(bind)

regex - Powershell中的RegEx用下划线替换XML元素名称中的空格

c# - 如何在 C# 中添加 ExcelPackage 引用

Nuget Update-Package 无法识别已安装的包 --> 更新失败