Visual Studio,如何使文件显示在列表框Powershell中

问题描述 投票:2回答:1

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

如果可能,我想使用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)

}

enter image description hereenter image description here

visual-studio powershell listbox
1个回答
1
投票

要向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布局的方法,但是如果没有,您可以使用我在本博客文章'Using GUI Elements in PowerShell'中介绍的方法。

我的listBox被称为$WPFlistBox,所以这是我的填充方式。

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

只需更改变量以匹配您自己的变量,就可以顺利进行。

这里是一个看起来像的例子。enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.