【问题标题】:Powershell get all filenames associated to a ProjectItemPowershell 获取与 ProjectItem 关联的所有文件名
【发布时间】:2017-01-03 20:37:38
【问题描述】:

我想用 powershell 获取 ProjectItem 下的所有文件

在上面我想要以下文件的路径:

  • Web.config
  • Web.Debug.config
  • Web.Release.config

我可以毫无问题地获得ProjectItem,但我不知道如何枚举 FileNames 数组。所有元素似乎都指向同一个东西,所以我想我一定是在枚举数组错误。

$projectItem.FileNames(0) => Path to web.config
$projectItem.FileNames(1) => Path to web.config

这里的任何数字似乎都返回相同的文件路径。

如何从 Powershell 中的 ProjectItem 中获取所有 3 个文件路径

要试试这个,把它放在一个 web 项目的包管理器控制台中:

@(Get-Project).ProjectItems | Where {$_.Name.StartsWith("Web") } | Select { $_.FileNames(0) }

【问题讨论】:

  • 来自文档:项目项从 1 到 FileCount 的文件名索引。愚蠢的问题,你试过FileNames(2)吗?
  • 使用方括号怎么样? $projectItem.FileNames[0]
  • 根据this question 括号应该可以工作。我已经安装了 Visual Studio,你能给出在 PS 中加载所需程序集的步骤来帮助你吗? :)
  • @sodawillow 是的,我尝试了一堆其他数字(2、-1 1000),它们都返回相同的东西
  • @sodawillow 它应该已经被 nuget 加载了,只需打开包管理器控制台并输入问题中的命令(查看 -> 其他 Windows -> 包管理器控制台,确保选择了一个 Web 项目从默认项目下拉菜单中)

标签: powershell nuget envdte


【解决方案1】:

知道了。基本上你必须扩展名为Web.configProjectItemProjectItems 集合。不过可能有更简单的方法。

$webconfig = @(Get-Project).ProjectItems |
    Where-Object { $_.Name -eq "Web.config" }

$webconfig.Properties("LocalPath").Value # path for web.config

$webdebugconfig = $webconfig.ProjectItems |
    Where-Object { $_.Name -eq "Web.Debug.config" }

$webdebugconfig.Properties("LocalPath").Value # path for web.debug.config

$webreleaseconfig = $webconfig.ProjectItems |
    Where-Object { $_.Name -eq "Web.Release.config" }

$webreleaseconfig.Properties("LocalPath").Value # path for web.release.config

【讨论】:

  • 你完全正确,我误解了 filenames 属性的作用。谢谢
猜你喜欢
  • 1970-01-01
  • 2020-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-10
  • 1970-01-01
  • 1970-01-01
  • 2019-09-11
相关资源
最近更新 更多