Vb.net 用目录内容填充列表框,但只接受某些扩展名

Vb.net 用目录内容填充列表框,但只接受某些扩展名,vb.net,path,drag-and-drop,listbox,getfiles,Vb.net,Path,Drag And Drop,Listbox,Getfiles,因此,基本上我将文件夹拖到表单上,列表框中填充了文件路径。我已经设法使列表框只接受.MP3路径,但是如何添加更多可接受的扩展 Private Sub Form1_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop Dim files() As String = e.Data.GetData(DataFormats.FileDrop)

因此,基本上我将文件夹拖到表单上,列表框中填充了文件路径。我已经设法使列表框只接受.MP3路径,但是如何添加更多可接受的扩展

 Private Sub Form1_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
            Dim files() As String = e.Data.GetData(DataFormats.FileDrop)
            For Each path In files

           If Directory.Exists(path) Then
                    'Add the contents of the folder to Listbox1
                    ListBox1.Items.AddRange(IO.Directory.GetFiles(path, "*.mp3*"))
正如您在上面最后一行中看到的,文件夹中扩展名为.mp3的路径是可以接受的。如何添加更多可接受的扩展,如.avi、.mp4等

我尝试了
ListBox1.Items.AddRange(IO.Directory.GetFiles(路径“*.mp3*”+“*.mp4*”)

我还尝试了
ListBox1.Items.AddRange(IO.Directory.GetFiles(路径“*.mp3*”,“*.mp4*”)


不走运

您应该创建for循环,测试扩展,然后添加或不添加它

有点像

    Dim AllowedExtension As String = "mp3 mp4"
    For Each file As String In IO.Directory.GetFiles("c:\", "*.*")
        If AllowedExtension.Contains(IO.Path.GetExtension(file).ToLower) Then
            listbox1.items.add(file)
        End If
    Next
甚至更脏

IO.Directory.GetFiles(path, "*.mp*")
或者做两次


我将
Dim AllowedExtension As String=“mp3.mp4”
修改为
Dim AllowedExtension As String=“.mp3.mp4”
,它可以工作。
     ListBox1.Items.AddRange(IO.Directory.GetFiles(path, "*.mp3*"))
     ListBox1.Items.AddRange(IO.Directory.GetFiles(path, "*.mp4*"))