【问题标题】:How to display selected file to a textbox GUI using powershell?如何使用 powershell 将所选文件显示到文本框 GUI?
【发布时间】:2019-06-10 16:05:36
【问题描述】:

我想从带有 GUI 的文件夹中选择一个文件,然后将所选文件名显示到一个文本框中。我试过了,但是选择文件后,它没有显示在文本框中。

 Function File ($InitialDirectory)
    {
        Add-Type -AssemblyName System.Windows.Forms
        $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
        $OpenFileDialog.Title = "Please Select File"
        $OpenFileDialog.InitialDirectory = $InitialDirectory
        $OpenFileDialog.filter = “All files (*.*)| *.*”
        If ($OpenFileDialog.ShowDialog() -eq "Cancel") 
        {
        [System.Windows.Forms.MessageBox]::Show("No File Selected. Please select a file !", "Error", 0, 
        [System.Windows.Forms.MessageBoxIcon]::Exclamation)
        }   $Global:SelectedFile = $OpenFileDialog.FileName

    } 

    Add-Type -AssemblyName System.Windows.Forms
    [System.Windows.Forms.Application]::EnableVisualStyles()

    $Form                            = New-Object system.Windows.Forms.Form
    $Form.ClientSize                 = '576,259'
    $Form.text                       = "Process"
    $Form.TopMost                    = $false
    #----------------------

    $ChooseML_L                      = New-Object system.Windows.Forms.Label
    $ChooseML_L.AutoSize             = $true
    $ChooseML_L.width                = 25
    $ChooseML_L.height               = 10
    $ChooseML_L.location             = New-Object System.Drawing.Point(128,45)
    $ChooseML_L.ForeColor            = "#000000"

    $SelectML                        = New-Object system.Windows.Forms.TextBox
    $SelectML.multiline              = $false
    $SelectML.width                  = 100
    $SelectML.height                 = 20
    $SelectML.location               = New-Object System.Drawing.Point(123,100)

    $ChooseML                        = New-Object System.Windows.Forms.Button
    $ChooseML.AutoSize               = $true
    $ChooseML.width                  = 100
    $ChooseML.height                 = 20
    $ChooseML.location               = New-Object System.Drawing.Point(123,69)
    $ChooseML.ForeColor              = "#ffffff"
    $ChooseML.BackColor              = "#093c76"

    $ChooseML.Add_Click({$SelectML.Text = File})

    $Form.Controls.AddRange(@($ChooseML, $ChooseML_L, $SelectML))
    [void] $Form.ShowDialog()

我的期望,当我选择文件后,它会显示到一个文本框。

【问题讨论】:

    标签: powershell user-interface


    【解决方案1】:

    这是因为您没有从 File Select 函数返回任何内容。

    只需将其添加到函数中即可。

    Function File ($InitialDirectory)
    {
        Add-Type -AssemblyName System.Windows.Forms
        $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
        $OpenFileDialog.Title = "Please Select File"
        $OpenFileDialog.InitialDirectory = $InitialDirectory
        $OpenFileDialog.filter = “All files (*.*)| *.*”
        If ($OpenFileDialog.ShowDialog() -eq "Cancel") 
        {
        [System.Windows.Forms.MessageBox]::Show("No File Selected. Please select a file !", "Error", 0, 
        [System.Windows.Forms.MessageBoxIcon]::Exclamation)
        }
        $Global:SelectedFile = $OpenFileDialog.FileName
        Return $SelectedFile #add this return
    } 
    

    要么这样,要么将$global:SelectedFile 的值分配给$SelectML.Text

    【讨论】:

    • 是的,我做到了。但它返回包含我选择的文件的路径。我只想显示没有路径的文件名。
    • @Job 您可以使用$Global:SelectedFile = $OpenFileDialog.SafeFileName 来获取文件名。如果您有多个文件返回,只需将其更改为 $Global:SelectedFile = $OpenFileDialog.SafeFileNames 这将返回一个数组。
    猜你喜欢
    • 2013-12-18
    • 1970-01-01
    • 2021-06-03
    • 1970-01-01
    • 2018-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多