2

所以,基本上我将一个文件夹拖到表单上,一个列表框会填充里面文件的路径。我已经设法让列表框只接受 .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(path, "*.mp3*" + "*.mp4*"))

我也试过ListBox1.Items.AddRange(IO.Directory.GetFiles(path, "*.mp3*" , "*.mp4*"))

没运气 !

4

1 回答 1

1

您应该创建一个 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*")

或者做两次;

添加

     ListBox1.Items.AddRange(IO.Directory.GetFiles(path, "*.mp3*"))

     ListBox1.Items.AddRange(IO.Directory.GetFiles(path, "*.mp4*"))
于 2013-08-16T13:10:46.563 回答