列出文件,按不带路径的扩展名过滤

List files, filtered by extension without path

由于难以在同一行代码中过滤和列出几种类型的文件,如下所示: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())

(但是不能过滤格式)不是吗?

您可以使用 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)