Visual Studio,如何让文件在 Listbox Powershell 中显示

Visual Studio, how to get files to show in Listbox Powershell

我创建了一个包含 2 个 pdf 文档的 PowerShell GUI,然后将其转换为 .tif,然后重命名并最终将其移动到最终位置。

如果可能的话,我想使用 ListBox 来显示上述脚本的操作

因此在第一个 ListBox 中它需要显示将要合并的 2 个 pdf 文件。

我似乎无法让它显示任何文件!

$listBox1_SelectedIndexChanged = {
    $listBox1.Text = Get-ChildItem "E:\SIGNEDNOTES" -Filter *.pdf
}
$listBox1_SelectedIndexChanged = {

    $rootFolder = "E:\SIGNEDNOTES\" 
    $subfolders = (Get-ChildItem -Path $rootFolder -Filter *.pdf -Recurse -Directory).FullName
    $listBox1 = New-Object System.Windows.Forms.ListBox
    $listBox1.Items.AddRange($subfolders)

}

要向 ListBox 添加新项目,您需要填充 ListBox.Items 集合。这是一个简单的例子。

鉴于此 XAML

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <ListBox x:Name="listBox" HorizontalAlignment="Left" Height="138" Margin="84,103,0,0" VerticalAlignment="Top" Width="332"/>

    </Grid>
</Window>

我假设您已经掌握了在 PowerShell 中使用 XAML 布局的方法,但如果您还没有,可以使用我在这篇博客 post 中介绍的方法'Using GUI Elements in PowerShell'.

我的 listBox 被称为 $WPFlistBox,所以我将如何填充它。

$items = get-childItem c:\temp\*.exe 
ForEach($item in $items){
   $WPFlistBox.Items.Add($item.FullName)
}   

只需更改变量以匹配您自己的变量,您应该一切顺利。

这是它的外观示例。