【发布时间】: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