ListBox中的FileExtension过滤器

时间:2015-07-22 13:38:43

标签: vb.net

所以这是我的小代码,只需一个按钮即可将项目添加到我的列表框中。

 FolderBrowserDialog1.ShowDialog()
    ListBox1.Items.Clear()
    FilePathLabel.Text = System.IO.Path.GetFileName(FolderBrowserDialog1.SelectedPath)
    Dim folder As New IO.DirectoryInfo(System.IO.Path.GetFullPath(FolderBrowserDialog1.SelectedPath))
    Dim arrfile() As IO.FileInfo
    Dim file As IO.FileInfo
    arrfile = folder.GetFiles("*.*")
    dicPaths.Clear()
    For Each file In arrfile
        'ListBox1.Items.Add(file.FullName)
        dicPaths.Add(file.Name, file.FullName)
    Next file
    For Each item As String In dicPaths.Keys
        ListBox1.Items.Add(item)
    Next item
    If CheckBox2.Checked Then
        FindFiles(FolderBrowserDialog1.SelectedPath)
    End If

    Label1.Text = "Total Items : " + ListBox1.Items.Count.ToString

我的问题是,如何过滤除扩展名以外的所有文件,例如.mp3和.mp4?我不希望有像.ink或.exe这样的扩展名的文件。我认为这是行

arrfile = folder.GetFiles("*.*")

但是我试着写* .mp3等,但它没有用。有人能帮助我吗?

3 个答案:

答案 0 :(得分:0)

您可以将您感兴趣的扩展名存储在一个数组中,并使用外部循环来检查每个扩展名。

FolderBrowserDialog1.ShowDialog()
ListBox1.Items.Clear()
FilePathLabel.Text = System.IO.Path.GetFileName(FolderBrowserDialog1.SelectedPath)
Dim folder As New IO.DirectoryInfo(System.IO.Path.GetFullPath(FolderBrowserDialog1.SelectedPath))
dicPaths.Clear()

'New code to search for multiple extensions
Dim exts() As String = {"*.mp3", "*.mp4"}
For Each ext As String In exts
    For Each myFile As IO.FileInfo In folder.GetFiles(ext)
        dicPaths.Add(myFile.Name, myFile.FullName)
    Next 
Next

For Each item As String In dicPaths.Keys
    ListBox1.Items.Add(item)
Next item
If CheckBox2.Checked Then
    FindFiles(FolderBrowserDialog1.SelectedPath)
End If

Label1.Text = "Total Items : " + ListBox1.Items.Count.ToString

答案 1 :(得分:0)

您可以使用LINQ过滤您感兴趣的文件:

Dim extensions() As String = { ".mp3", ".mp4" }
Dim arrfile() As FileInfo = (
    From fileInfo In folder.EnumerateFiles()
    Where extensions.Contains(fileInfo.Extension)
).ToArray()

答案 2 :(得分:0)

GetFiles支持* ?,因此您可以这样做:

arrfile = folder.GetFiles("*.mp?")

附注:只需通过DataSource将FileInfo实例直接添加到ListBox,然后设置DisplayMember和ValueMember值:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
        Dim folder As New IO.DirectoryInfo(FolderBrowserDialog1.SelectedPath)
        Dim files As New List(Of IO.FileInfo)(folder.GetFiles("*.mp?"))
        ListBox1.DataSource = files
        ListBox1.DisplayMember = "Name"
        ListBox1.ValueMember = "FullName"

        If CheckBox2.Checked Then
            FindFiles(FolderBrowserDialog1.SelectedPath)
        End If

        FilePathLabel.Text = "Total Items : " + ListBox1.Items.Count.ToString
    End If
End Sub

Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
    lblFullname.Text = ListBox1.SelectedValue.ToString
    Dim fi As IO.FileInfo = ListBox1.SelectedItem
    ' ... possibly do something with "fi" ...
    Debug.Print(fi.FullName)
End Sub

现在您可以访问代表当前所选项的实际FileInfo实例。你可以用它做任何你想做的事......