所以,基本上我把一个文件夹拖到表单上,一个列表框就会填充里面文件的路径。我已经设法使列表框只接受.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*"))
不走运!
发布于 2013-08-16 13:10:47
您应该创建一个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*"))
https://stackoverflow.com/questions/18273850
复制相似问题