openFileDialog 过滤器,排除带有序列号后缀的文件

问题描述 投票:0回答:1

我在文件夹中缩进了文件。

  • 文件1.txt
  • 文件1.txt.1
  • 文件1.txt.2
  • ...
  • 文件1.txt.x

如何在system.Windows.Forms.OpenFileDialog中设置过滤器

openFileDialog.Filter = "txt files (.txt)|.txt"

只显示没有缩进的文件?不显示文件.txt.1、.txt.2

只是file.txt

谢谢。

powershell openfiledialog
1个回答
0
投票

您问题中的

.Filter
值有缺陷,因为
|
之后的部分必须是 通配符 模式(例如
*.txt
,而不仅仅是 扩展名 (
.txt
)。

问题得到纠正后,行为将达到预期效果,如以下代码所示(它作用于 current 位置(目录);根据需要调整

.InitialDirectory
值:

# Load the WinForms assembly.
# Required in Windows PowerShell only.
Add-Type -Assembly System.Windows.Forms 

# Create an instance of the dialog class and initialize it.
$dlg = [System.Windows.Forms.OpenFileDialog]::new()
$dlg.InitialDirectory = $PWD.ProviderPath
$dlg.Filter = 'Text files (.txt)|*.txt' # NOTE: '|*.txt', not '|.txt'

# Show the dialog
$result = $dlg.ShowDialog()

if ($result -eq 'OK') {
  $selectedFile = $dlg.FileNames[0]
  Write-Verbose -Verbose "You selected: $selectedFile"
} else {
  Write-Warning 'Dialog was canceled.'
}
© www.soinside.com 2019 - 2024. All rights reserved.