Powershell Filebrowser文件名不用于获取内容
我的表单上有以下代码,用于获取用户选择的文件名,然后对该文件中的用户名执行操作。然而,我被困在变量(从filebrowser创建)中,无论发生什么情况,用来获取内容的变量都无法工作。两种代码(工作和非工作,如所附) 当我在代码的后面部分打印$filepath的值时,它会正确地打印为c:\scripts\testusers.txt。请让我知道为什么这个安排行不通Powershell Filebrowser文件名不用于获取内容,powershell,Powershell,我的表单上有以下代码,用于获取用户选择的文件名,然后对该文件中的用户名执行操作。然而,我被困在变量(从filebrowser创建)中,无论发生什么情况,用来获取内容的变量都无法工作。两种代码(工作和非工作,如所附) 当我在代码的后面部分打印$filepath的值时,它会正确地打印为c:\scripts\testusers.txt。请让我知道为什么这个安排行不通 #------------- Form Code ------- $cmdFile.Add_Click({ #N
#------------- Form Code -------
$cmdFile.Add_Click({
#Note: I like to define the $FileBrowser outside of click event
# so it can be reused without reinitializing
$FileBrowser.Filter = "Text Files|*.txt;*.csv|All Files|*.*"
If ($FileBrowser.ShowDialog() -eq "Cancel") {
$filepath = ""
}
Else {
$filepath = $FileBrowser.Filename
$FileBrowser.FileName = $Null
$FileBrowser.Filter = ""
#Are you really using SelectedFile as a Global, multiple scripts?
#or just in this script which would then be $Script vs $Global.
# I'd use this to see if the problem is here or later where the
#Global is used.
$Global:SelectedFile = "$filePath"
$users = get-content -path "$filepath"
foreach ($u in $users) {
### control does not flow to this part of code
}
}
})
#---------- Main Code --------------
$FileBrowser = New-Object Windows.Forms.OpenFileDialog
嗯
HTH如前所述,从不从内存中清除$FileBrowser对话框对象,最终会遇到问题。 此外,尽管我无法通过查看您的问题来了解将所选文件存储在全局变量中的原因,但您需要在每次输入click事件时清除该全局变量,以防止在其中保留过时的信息 试一试
另外,通过将
Multiselect
属性设置为$false
可以告诉对话框在其$FileBrowser.Filename
属性中只接受并返回一个文件。如果设置为$true
,则所选文件将通过$FileBrowser.Filenames
(注意“s”的复数形式)返回。如注释所示,如果从未从内存中清除$FileBrowser对话框对象,则最终会遇到问题。
此外,尽管我无法通过查看您的问题来了解将所选文件存储在全局变量中的原因,但您需要在每次输入click事件时清除该全局变量,以防止在其中保留过时的信息
试一试
另外,通过将
Multiselect
属性设置为$false
可以告诉对话框在其$FileBrowser.Filename
属性中只接受并返回一个文件。如果设置为$true
,则所选文件将通过$FileBrowser.Filenames
(注意“s”的复数形式)返回,假设$FileBrowser
是一个Windows.Forms.OpenFileDialog
,您需要显示使用该代码的位置。请添加完整代码以供阅读$filepath=$SelectedFile?如果您将-encoding ascii
或-encoding utf8
添加到获取内容$filepath=$SelectedFile会怎么样?(已经试过)运气不好。编码没有帮助。假设$FileBrowser
是一个Windows.Forms.OpenFileDialog
,您需要显示使用该对话框的代码。添加完整代码以供阅读请$filepath=$SelectedFile?如果您将-encoding ascii
或-encoding utf8
添加到获取内容$filepath=$SelectedFile会怎么样?(已经试过)运气不好。编码没有帮助我使用全局,因为它将用于多个单击事件。问题不在于global:SelectedFile变量,因为当我使用write host$filepath时,它会输出正确的值。神秘之处在于:当我将$filepath设置为C:\scripts\testusers.txt时,get content-path$filepath成功读取文件。当我将$filepath设置为$Global:SelectedFile,然后使用get content-path$filepath时,代码不会读取文件,并且不会输出任何内容,因为它将在多个单击事件中使用。问题不在于global:SelectedFile变量,因为当我使用write host$filepath时,它会输出正确的值。神秘之处在于:当我将$filepath设置为C:\scripts\testusers.txt时,get content-path$filepath成功读取文件。当我将$filepath设置为$Global:SelectedFile,然后使用get-content-path$filepath时,代码不会读取文件,也不会输出任何内容。我在20多个点击事件中使用了全局变量,结果它被污染了。谢谢大家。我因为被怀疑是新冠病毒而被隔离。但是我是负数,所以你都安全了;)@谢谢你让我们知道。很高兴你也很安全;)是的,先生。我在20多个点击事件中使用了全局变量,结果它被污染了。谢谢大家。我因为被怀疑是新冠病毒而被隔离。但是我是负数,所以你都安全了;)@谢谢你让我们知道。很高兴你也很安全;)
#------------- Form Code -------
$cmdFile.Add_Click({
#Note: I like to define the $FileBrowser outside of click event
# so it can be reused without reinitializing
$FileBrowser.Filter = "Text Files|*.txt;*.csv|All Files|*.*"
If ($FileBrowser.ShowDialog() -eq "Cancel") {
$filepath = ""
}
Else {
$filepath = $FileBrowser.Filename
$FileBrowser.FileName = $Null
$FileBrowser.Filter = ""
#Are you really using SelectedFile as a Global, multiple scripts?
#or just in this script which would then be $Script vs $Global.
# I'd use this to see if the problem is here or later where the
#Global is used.
$Global:SelectedFile = "$filePath"
$users = get-content -path "$filepath"
foreach ($u in $users) {
### control does not flow to this part of code
}
}
})
#---------- Main Code --------------
$FileBrowser = New-Object Windows.Forms.OpenFileDialog
$cmdFile.Add_Click({
# start by clearing the $Global:SelectedFile variable
$Global:SelectedFile = $null
$FileBrowser = New-Object Windows.Forms.OpenFileDialog
$FileBrowser.Filter = "Text Files|*.txt;*.csv|All Files|*.*"
$FileBrowser.Multiselect = $false
$FileBrowser.CheckFileExists = $true
$null = $FileBrowser.ShowDialog()
# get the selected filename if any
$filePath = $FileBrowser.Filename
# clean up the dialog object from memory
$FileBrowser.Dispose()
# check if you received a file name
if (![string]::IsNullOrWhiteSpace($filePath)) {
# you can set your global variable here if you must
$Global:SelectedFile = $filePath
Get-Content -Path $filepath | ForEach-Object {
# perform an action on each username in the file
# each line of the file is represented by the $_ automatic variable
}
}
})