在同一行代码中过滤和列出几种类型的文件会很困难,例如:System.IO.directory.getfiles(path, "*.avi, *.flv, *mpg"
我使用排序的ListBox,下一个代码为每种格式添加一行:
Dim newdir As String
ListBox3.Items.AddRange(System.IO.Directory.GetFiles(newdir, "*.avi"))
ListBox3.Items.AddRange(System.IO.Directory.GetFiles(newdir, "*.flv"))
ListBox3.Items.AddRange(System.IO.Directory.GetFiles(newdir, "*.mpg"))
此代码列表按字母顺序只知道VIDEO文件,唯一的问题是:它列出了包含扩展名的完整路径!我如何管理这个只是在没有扩展名的情况下获得最简单的代码?我的意思是适应.GetFileNameWithoutExtension
的结构:
IO.Path.GetFileNameWithoutExtension())
(但它不能过滤格式)不是吗?
答案 0 :(得分:2)
您可以使用LINQ,如下所示:
Dim files As New List(Of String)
files.AddRange(IO.Directory.GetFiles(newdir, "*.avi").
Select(Function(f) IO.Path.GetFileNameWithoutExtension(f)))
files.AddRange(IO.Directory.GetFiles(newdir, "*.flv").
Select(Function(f) IO.Path.GetFileNameWithoutExtension(f)))
files.AddRange(IO.Directory.GetFiles(newdir, "*.mpg").
Select(Function(f) IO.Path.GetFileNameWithoutExtension(f)))
ListBox3.Items.AddRange(files.ToArray)