将文件路径存储为变量
Storing the File Path as a variable
我正在尝试通过 Powershell 创建一个 Windows 表单,我需要捕获文件路径并将其存储在一个变量中。用户点击 'Select' 按钮并选择文件后,我想将文件路径存储在一个变量中。有人可以帮我吗?显示文件路径的代码部分是 $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()
在您的 .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 作用域中 - 参见 。
这是您的代码的独立改编版本,它说明了 post-.ShowDialog()
方法:
Main-Program
调用移至 .ShowDialog()
调用之后和 $selectedFilePath
变量赋值之后。
删除了 $okButton.Add_Click()
事件处理程序,取而代之的是 $okButton.DialogResult = 'OK'
,这使得按钮 自动 关闭表单并使 .ShowDialog()
return [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.'
}
我正在尝试通过 Powershell 创建一个 Windows 表单,我需要捕获文件路径并将其存储在一个变量中。用户点击 'Select' 按钮并选择文件后,我想将文件路径存储在一个变量中。有人可以帮我吗?显示文件路径的代码部分是 $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()
在您的 .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 作用域中 - 参见
这是您的代码的独立改编版本,它说明了 post-.ShowDialog()
方法:
Main-Program
调用移至.ShowDialog()
调用之后和$selectedFilePath
变量赋值之后。删除了
$okButton.Add_Click()
事件处理程序,取而代之的是$okButton.DialogResult = 'OK'
,这使得按钮 自动 关闭表单并使.ShowDialog()
return[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.'
}