我试图理解为什么我无法使用.GetFiles
返回多个文件扩展名。
这是我当前的代码,工作正常。
'Returns only the filenames based on the directory that is selected
Dim fi = From f In New IO.DirectoryInfo(FolderBrowserDialog1.SelectedPath).GetFiles("*.txt").Cast(Of IO.FileInfo)() _
Order By f.CreationTime
Select f
For Each fileInfo As System.IO.FileInfo In fi
ListBoxFileAvailable.Items.Add(fileInfo.Name)
Next
当我运行它时,我的列表框只填充了* .txt文件。
以下是我添加("*.txt, *.xlsx")
并再次运行代码的代码,我的列表框中没有填充任何内容。
'Returns only the filenames based on the directory that is selected
Dim fi = From f In New IO.DirectoryInfo(FolderBrowserDialog1.SelectedPath).GetFiles("*.txt, *.xlsx").Cast(Of IO.FileInfo)() _
Order By f.CreationTime
Select f
For Each fileInfo As System.IO.FileInfo In fi
ListBoxFileAvailable.Items.Add(fileInfo.Name)
Next
如何在列表框中添加多个文件扩展名?
答案 0 :(得分:2)
基本上您正在寻找一个如下所示的文件:somefile.txt, *.xlxs
这是不可能的,因为您在Windows文件名中不能有*
。而是为每个扩展程序构建一个所有文件的列表
Dim extList() As String = {"*.txt", "*.xlxs"} ' I think you actually want .xlsx but whatever...
Dim fileList As New List(Of FileInfo)
For Each ext In extList
'Returns only the filenames based on the directory that is selected
Dim fi = From f In New IO.DirectoryInfo(FolderBrowserDialog1.SelectedPath).GetFiles(ext).Cast(Of IO.FileInfo)() Order By f.CreationTime Select f
fileList.AddRange(fi)
Next
For Each fileInfo As System.IO.FileInfo In fileList
ListBoxFileAvailable.Items.Add(fileInfo.Name)
Next
答案 1 :(得分:2)
这是我想出的,效果很好。我添加了其中f.Extension =" .txt" OrElse f.Extension =" .xlsx"
'Returns only the filenames based on the directory that is selected
Dim fi = From f In New IO.DirectoryInfo(FolderBrowserDialog1.SelectedPath).GetFiles().Cast(Of IO.FileInfo)() _
Where f.Extension = ".txt" OrElse f.Extension = ".xlsx"
Order By f.Name
Select f
For Each fileInfo As System.IO.FileInfo In fi
ListBoxFileAvailable.Items.Add(fileInfo.Name)
Next
答案 2 :(得分:0)
尝试一下,效果很好。下面的代码列出了* .jpg和* .png(Vb.net 2015)
'If option strict off ... use these
Dim xtn As Array = {"*.jpg", "*.png"}
For i = 0 To 1
For Each foundfile As String In Directory.GetFiles("d:\pic", xtn(i))
ListBox1.Items.Add(foundfile)
Next
Next
'If option strict on ... use below intead
Dim xtn(0 To 2) As String
xtn(0) = "*.jpg"
xtn(1) = "*.png"
xtn(2) = "*.jpeg"
For i = 0 To 2
For Each foundfile As String In Directory.GetFiles(fld, xtn(i))
ListBox5.Items.Add(foundfile)
Next
Next