【问题标题】:Dynamically create listviewgroup powershell动态创建listviewgroup powershell
【发布时间】:2021-01-02 06:32:12
【问题描述】:

我有一个列表视图,我通过运行 Get-ChildItem 来填充文件夹的内容。我想根据它们所在的文件夹将项目与 listviewgroup 分组。我的问题是我似乎无法动态地做到这一点。

例如,我使用 Get-ChildItem 5 个文件夹,即:

\\1
\\2
\\3
\\4
\\5

所以我想创建 5 个组等。下面是我到目前为止尝试的代码:

$getgroups = Get-ChildItem $path -Filter $whattosearchfor -Recurse:$recurse | % {split-path $_.FullName } | Sort-Object -Descending | select-object -Unique

  for($testingI = 1;$testingI -lt $getgroups.length;$testingI++){
      $testingII = $testingI
      $testingII     = New-Object System.Windows.Forms.ListViewGroup
      $testingII.Header = $_
      $testingII.Name   = $_
      [system.void]$listView1.Groups.Add($testingII)
  }
  #$getgroups | % {
  #    #$xCounter        = $_ 
  #    $xCounter        = New-Object System.Windows.Forms.ListViewGroup
  #    $xCounter.Header = $_
  #    $xcounter.Name   = $_
  #    $listView1.Groups.Add($xCounter)
  #    $x++
  # }

【问题讨论】:

  • 找到了一个解决办法,使用hashtables版主可以关闭线程thxxx
  • 您可以发布您的解决方案以帮助遇到相同或类似问题的其他人,或者如果您认为它对其他人没有帮助,您可以删除您的问题。 ;-)
  • ...或者,如果您不想帮助他人,您可以删除您的问题。您不需要版主。

标签: powershell powershell-2.0 powershell-3.0 powershell-4.0


【解决方案1】:

对不起

解决办法

    $xcounter = @{ }
    $getgroups = Get-ChildItem $path -Filter $whattosearchfor -Recurse:$recurse | % { split-path $_.FullName } | Sort-Object -Descending | select-object -Unique
    $getgroups | % {
        $temp = New-Object System.Windows.Forms.ListViewGroup
        $temp.Header = $_
        $temp.Name = $_
        $xCounter[$_] = $temp
        $listView1.Groups.Add($xCounter[$_])
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-09
    • 1970-01-01
    • 1970-01-01
    • 2021-03-18
    • 2021-11-29
    • 2014-02-24
    • 1970-01-01
    • 2023-03-09
    相关资源
    最近更新 更多