【问题标题】:Get Files With Multiple Extensions And Adding Them To Listbox获取具有多个扩展名的文件并将它们添加到列表框
【发布时间】:2014-06-17 14:55:37
【问题描述】:

我试图了解为什么我不能使用.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

如何在我的列表框中添加多个文件扩展名?

【问题讨论】:

    标签: vb.net listbox file-extension


    【解决方案1】:

    基本上,您正在寻找一个看起来像这样的文件: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
    

    【讨论】:

      【解决方案2】:

      这是我想出的,效果很好。我添加了 Where 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
      

      【讨论】:

        【解决方案3】:

        试试这个,效果很好。下面列出 *.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
        

        【讨论】:

          猜你喜欢
          • 2017-08-31
          • 2016-09-06
          • 1970-01-01
          • 2023-02-01
          • 2020-07-28
          • 1970-01-01
          • 1970-01-01
          • 2017-10-04
          • 1970-01-01
          相关资源
          最近更新 更多