PowerShell - 列出 TreeView GUI 元素中的所有文件夹和子文件夹

溴257

我正在尝试在 TreeView PowerShell 元素中列出驱动器 C: 的所有目录和子目录。

目前它只列出一个子文件夹实例,但我无法选择这些子文件夹来查看其中的目录。

这是代码:

function tree {

   [void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
   [void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

   $Form = New-Object System.Windows.Forms.Form
   $Form.Text = "Files"
   $Form.Size = New-Object System.Drawing.Size(390, 390)

   $TreeView = New-Object System.Windows.Forms.TreeView
   $TreeView.Location = New-Object System.Drawing.Point(48, 12)
   $TreeView.Size = New-Object System.Drawing.Size(290, 322)
   $Form.Controls.Add($TreeView)


   $rootnode = New-Object System.Windows.Forms.TreeNode
   $rootnode.text = "Root"
   $rootnode.name = "Root"
   [void]$TreeView.Nodes.Add($rootnode)


   [email protected](Get-ChildItem -Path C:\Users)
   Write-Host $array
   foreach ( $obj in $array ) {                                                                                                             
        Write-Host $obj
        $subnode = New-Object System.Windows.Forms.TreeNode
        $subnode.text = $obj
        [void]$rootnode.Nodes.Add($subnode)
     }

   $Form.Add_Shown({$Form.Activate()})
   [void] $Form.ShowDialog()

   }
   tree

这是运行中的 GUI:GUI

感谢所有为这个问题做出贡献的人。

跟随

您可以在主函数中插入一个函数,该函数以递归方式向树视图添加节点。

在下面的代码,我已经改变了你的函数的名称treeShow-DirectoryTree拥有它符合Verb-Noun的PowerShell函数的命名约定。

此外,如果硬编码根文件夹路径,我向RootFolder函数添加了一个参数,使其在使用中更加灵活。

function Show-DirectoryTree {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Position = 0)]
        [string]$RootFolder
    )

    # recursive helper function to add folder nodes to the treeview
    function Add-Node {
        param (
            [System.Windows.Forms.TreeNode]$parentNode, 
            [System.IO.DirectoryInfo]$Folder
        )
        Write-Verbose "Adding node $($Folder.Name)"
        $subnode      = New-Object System.Windows.Forms.TreeNode
        $subnode.Text = $Folder.Name
        [void]$parentNode.Nodes.Add($subnode)
        Get-ChildItem -Path $Folder.FullName -Directory | ForEach-Object {
            Add-Node $subnode $_
        }
    }

    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing

    $Form = New-Object System.Windows.Forms.Form
    $Form.Text = "Folders"
    $Form.Size = New-Object System.Drawing.Size(390, 390)

    $TreeView = New-Object System.Windows.Forms.TreeView
    $TreeView.Location = New-Object System.Drawing.Point(48, 12)
    $TreeView.Size = New-Object System.Drawing.Size(290, 322)
    $Form.Controls.Add($TreeView)

    $rootnode = New-Object System.Windows.Forms.TreeNode
    # get the name of the rootfolder to use for the root node
    $rootnode.Text = [System.IO.Path]::GetFileName($RootFolder.TrimEnd('\'))  #'# or use: (Get-Item -Path $RootFolder).Name
    $rootnode.Name = "Root"
    [void]$TreeView.Nodes.Add($rootnode)
    # expand just the root node
    $rootNode.Expand()

    # get all subdirectories inside the root folder and 
    # call the recursive function on each of them
    Get-ChildItem -Path $RootFolder -Directory | ForEach-Object {
        Add-Node $rootnode $_
   }

   $Form.Add_Shown({$Form.Activate()})
   [void] $Form.ShowDialog()

   # remove the form when done with it
   $Form.Dispose()
}

# call the function to show the directory tree 
# take off the -Verbose switch if you do not want console output
Show-DirectoryTree -RootFolder 'C:\Users' -Verbose

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章