如何使用powershell将所选文件显示到textbox GUI?
我想用GUI从文件夹中选择一个文件,然后在文本框中显示所选文件名。我尝试过,但在选择文件后,它不会显示在文本框中如何使用powershell将所选文件显示到textbox GUI?,powershell,user-interface,Powershell,User Interface,我想用GUI从文件夹中选择一个文件,然后在文本框中显示所选文件名。我尝试过,但在选择文件后,它不会显示在文本框中 Function File ($InitialDirectory) { Add-Type -AssemblyName System.Windows.Forms $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog $OpenFileDialog.Tit
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()
我的期望是,在我选择文件后,它将显示在一个文本框中。这是因为您没有从
文件
选择功能返回任何内容
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()
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
}
只需将其添加到函数中
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
这是因为您没有从文件
选择函数返回任何内容
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
}
只需将其添加到函数中
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
是的,我这样做了。但它会返回我选择的文件的路径。我只想显示不带路径的文件名。@作业您可以使用$Global:SelectedFile=$OpenFileDialog.SafeFileName
仅获取文件名。如果有多个文件返回,只需将其更改为将返回数组的$Global:SelectedFile=$OpenFileDialog.SafeFileNames
。是的,我这样做了。但它会返回我选择的文件的路径。我只想显示不带路径的文件名。@作业您可以使用$Global:SelectedFile=$OpenFileDialog.SafeFileName
仅获取文件名。如果有多个文件返回,只需将其更改为将返回数组的$Global:SelectedFile=$OpenFileDialog.SafeFileNames
。