【问题标题】:Storing the File Path as a variable将文件路径存储为变量
【发布时间】:2021-08-31 11:38:45
【问题描述】:

我正在尝试通过 Powershell 创建一个 Windows 窗体,我需要捕获文件路径并将其存储在一个变量中。用户单击“选择”按钮并选择文件后,我想将文件路径存储在变量中。有人可以帮我吗?显示文件路径的部分代码是 $selectButton.Add_Click() 方法。

    $folderForm = New-Object System.Windows.Forms.Form
    $pathTextBox = New-Object System.Windows.Forms.TextBox
    $fileBrowser = New-Object System.Windows.Forms.OpenFileDialog
    $selectButton = New-Object System.Windows.Forms.Button
    $okButton = New-Object System.Windows.Forms.Button
    $cancelButton = New-Object System.Windows.Forms.Button
    $WarningText = New-Object System.Windows.Forms.Label
    $FormTitleText = New-Object System.Windows.Forms.Label
    $PathLabel = New-Object System.Windows.Forms.Label
    $FormTitle = New-Object System.Windows.Forms.Label

    $folderForm.Width = 650
    $folderForm.Height = 410
    $folderForm.Text = 'Private IP Program'

    $FormTitle.Text = "Verizon Private IP Network Diagram"
    $FormTitle.Location = '175,10'

    $PathLabel.Text = 'Path:'
    $PathLabel.Location = '5,40'
    $pathTextBox.Size = '495,20'
    $pathTextBox.Location = '50,40'

    $selectButton.Location = '550,40'
    $WarningText.Location = '5, 100'

    $FormTitle.Text = "Verizon Private IP Network Diagram"
    $FormTitle.Location = '175,10'

    $WarningText.AutoSize = $true
    $FormTitle.AutoSize = $true

    $folderForm.Controls.Add($WarningText)
    $folderForm.Controls.Add($pathTextBox)
    $folderForm.Controls.Add($selectButton)
    $folderForm.Controls.Add($PathLabel)
    $folderForm.Controls.Add($FormTitle)

    $selectButton.Text = 'Select'
    $WarningText.Text = "Warning: Please note that manual arrangement of locations is needed after 30 sites."

    $selectButton.Add_Click({
        $fileBrowser.ShowDialog()
        $pathTextBox.Text = $fileBrowser.FileName
    })

    #$pathTextBox.ReadOnly = $true
    #$test.ReadOnly = $true

    $okButton.Text = "Ok"
    $okButton.Location = '225,200'

    $cancelButton.Text = "Cancel"
    $cancelButton.Location = "325,200"

    $folderForm.AcceptButton = $okButton
    $folderForm.CancelButton = $cancelButton

    $okButton.Add_Click({
        Main-Program
        $folderForm.Close()
    })

    $folderForm.Controls.Add($okButton)
    $folderForm.Controls.Add($cancelButton)

    $folderForm.ShowDialog()

【问题讨论】:

    标签: powershell winforms powershell-4.0


    【解决方案1】:

    在您的.ShowDialog() 调用之后,您可以简单地查询您的$pathTextBox 文本框对象的值。

    # ...
    
    # Show the dialog modally and wait for it to close.
    $null = $folderForm.ShowDialog()
    
    # Get the selected path from the textbox and store it in a variable.
    # You probably want to verify that the value is valid (not empty and
    # refers to an existing file).
    $selectedFilePath = $pathTextBox.Text
    
    # Save it to a file.
    $selectedFilePath > SelectedFilePath.txt
    

    注意:如果您确实需要在对话框关闭之前填充变量,请使用$script:selectedFilePath 在您的$selectButton.Add_Click() 内的脚本 范围内定义一个变量脚本块,这是必要的,因为事件处理程序脚本块在 child 范围内运行 - 请参阅 this answer


    这是一个独立的、经过改编的代码版本,用于说明 post-.ShowDialog() 方法:

    • Main-Program 调用移至.ShowDialog() 调用之后和$selectedFilePath 变量赋值之后。

    • 删除了$okButton.Add_Click() 事件处理程序以支持$okButton.DialogResult = 'OK',这使得按钮自动关闭 并使.ShowDialog() 返回[System.Windows.Forms.DialogResult]::OK

    • 代码还展示了如何测试对话框是否通过确定按钮关闭,并确保路径不为空或空白。

    Add-Type -AssemblyName System.Windows.Forms
    
    $folderForm = New-Object System.Windows.Forms.Form
    $pathTextBox = New-Object System.Windows.Forms.TextBox
    $fileBrowser = New-Object System.Windows.Forms.OpenFileDialog
    $selectButton = New-Object System.Windows.Forms.Button
    $okButton = New-Object System.Windows.Forms.Button
    $cancelButton = New-Object System.Windows.Forms.Button
    $WarningText = New-Object System.Windows.Forms.Label
    $FormTitleText = New-Object System.Windows.Forms.Label
    $PathLabel = New-Object System.Windows.Forms.Label
    $FormTitle = New-Object System.Windows.Forms.Label
    
    $folderForm.Width = 650
    $folderForm.Height = 410
    $folderForm.Text = 'Private IP Program'
    
    $FormTitle.Text = "Verizon Private IP Network Diagram"
    $FormTitle.Location = '175,10'
    
    $PathLabel.Text = 'Path:'
    $PathLabel.Location = '5,40'
    $pathTextBox.Size = '495,20'
    $pathTextBox.Location = '50,40'
    
    $selectButton.Location = '550,40'
    $WarningText.Location = '5, 100'
    
    $FormTitle.Text = "Verizon Private IP Network Diagram"
    $FormTitle.Location = '175,10'
    
    $WarningText.AutoSize = $true
    $FormTitle.AutoSize = $true
    
    $folderForm.Controls.Add($WarningText)
    $folderForm.Controls.Add($pathTextBox)
    $folderForm.Controls.Add($selectButton)
    $folderForm.Controls.Add($PathLabel)
    $folderForm.Controls.Add($FormTitle)
    
    $selectButton.Text = 'Select'
    $WarningText.Text = "Warning: Please note that manual arrangement of locations is needed after 30 sites."
    
    $selectButton.Add_Click( {
            $fileBrowser.ShowDialog()
            $pathTextBox.Text = $fileBrowser.FileName
        })
    
    $okButton.Text = "Ok"
    $okButton.Location = '225,200'
    # This ensures that the button closes the form and
    # that .ShowDialog() reports [System.Windows.Forms.DialogResult]::OK
    $okButton.DialogResult = 'OK'
    
    $cancelButton.Text = "Cancel"
    $cancelButton.Location = "325,200"
    
    $folderForm.AcceptButton = $okButton
    $folderForm.CancelButton = $cancelButton
    
    $folderForm.Controls.Add($okButton)
    $folderForm.Controls.Add($cancelButton)
    
    if ($folderForm.ShowDialog() -eq 'OK' -and $pathTextBox.Text.Trim()) {
        $selectedFilePath = $pathTextBox.Text.Trim()
        $selectedFilePath > SelectedPath.txt
        Write-Verbose -vb "Proceeding with path $selectedFilePath..."
        # ... call code that relies on $selectedFilePath or to which pass it as an argument.
        Main-Program
    }
    else {
        Write-Warning 'Canceled or no path specified.'
    }
    

    【讨论】:

    • 很高兴听到这个消息,@legendaryspartan98;我的荣幸。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-06
    • 1970-01-01
    • 2021-05-01
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多