问题描述
我是 Nuget 的新手,我正在尝试上传我的第一个包.到目前为止,一切都很顺利.但是,我正在尝试在一些我希望位于 Lib 子文件夹中的内容文件上设置 CopyToOutputDirectory.我的目录如下所示:
I'm new to Nuget and I'm trying to figure out uploading my first package. So far, everything's gone smoothly. However, I'm trying to set CopyToOutputDirectory on some content files that I want to live in a Lib subfolder. My directory looks like this:
│ Readme.txt
│ MyPackage.nupkg
│ MyPackage.nuspec
│
├───content
│ └───Lib
│ native1.dll
│ native2.dll
│ native3.dll
│ native4.dll
│
├───lib
│ MyActualAssembly.dll
│
└───tools
Install.ps1
从阅读这个 StackOverflow 问题 和一些额外的阅读,我已经把一个 Install.ps1 放在一起,看起来像这样:
From reading this StackOverflow question and some additional reading, I've put together an Install.ps1 that looks like this:
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 线排列,以查看它是否有助于我理解问题,但其他方面的答案几乎与答案相同.
I 1-lined the various operations to see if it helped me understand the problem, but it's virtually the same as that answer otherwise.
根据我的测试,Install.ps1 在查找文件时遇到了一些麻烦.安装包后运行时,出现以下错误:
From my tests, the Install.ps1 is having some trouble with finding the files themselves. When it runs after installing the package, I get the following errors:
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 设置都设置为默认设置为不复制".
And, as you'd expect, all the files have their CopyToOutputDirectory setting set as Do Not Copy, the default.
我该如何解决这个问题?在 ps 脚本中访问子文件夹是否有不同的语法?还是我完全忽略了这些错误消息的要点?
How do I resolve this? Is there a different syntax for accessing subfolders in ps scripts? Or am I completely missing the point of those error messages?
推荐答案
尝试以下方法:
$project.ProjectItems.Item("Lib").ProjectItems.Item("native1.dll").Properties.Item("CopyToOutputDirectory").Value = 1
我可能是错的,但我认为 ProjectItems 不会允许您定位不是当前项目的直接子项的项目.所以你需要先找到Lib文件夹项目项,然后在这个项目项中查找你的dll.
I could be wrong but I do not think that ProjectItems will allow you to locate items that are not direct children of the current item. So you need to find the Lib folder project item first and then look inside this project item for your dll.
为了测试这些,我通常打开包管理器控制台窗口,确保在默认项目下拉列表中选择了正确的项目,然后使用命令行访问项目对象:
To test these I usually open up the Package Manager Console window, make sure the correct project is selected in the Default project drop down list, and then access the project object by using the command line:
$project = 获取项目
$project = Get-Project
这为您提供与 NuGet 安装脚本相同的功能,即项目的 Visual Studio 对象模型.
This gives you the same thing as the NuGet install script does which is the Visual Studio object model for the project.
这篇关于Nuget - 在子文件夹中的内容上设置 CopyToOutputDirectory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!