使用Directory.GetFiles()WITH多个扩展和排序顺序

时间:2013-09-01 10:18:52

标签: vb.net sorting getfiles

我必须得到一个目录文件列表,在多个扩展名上过滤......并排序!

我使用这个,这是我发现在多个扩展程序上过滤dir内容的最快方法:

Dim ext As String() = {"*.jpg", "*.bmp","*png"}
Dim files As String() = ext.SelectMany(Function(f) Directory.GetFiles(romPath, f)).ToArray
Array.Sort(files)

然后使用数组排序。

我想知道(这是我的问题;))是否有办法在同一主线上进行排序?一种:

Dim files As String() = ext.SelectMany(Function(f) Directory.GetFiles(romPath, f).**Order By Name**).ToArray

并且,如果是的话,如果我在这方面获得速度而不是在最后对数组进行排序(但我会做我的测试并报告......一旦我得到解决方案!!)? 谢谢你的帮助!!

2 个答案:

答案 0 :(得分:3)

您可以使用OrderBy()Linq扩展方法,如下所示:

    Dim ext = {"*.jpg", "*.bmp", "*png"}
    Dim files = ext.SelectMany(Function(f) Directory.GetFiles(romPath, f)). _
                OrderBy(Function(f) f). _
                ToArray()

它对速度没有任何影响,排序本质上是O(nlog(n))的复杂性。 在存储中造成差异,OrderBy()具有O(n)存储要求。 Array.Sort()就地排序。对于小n值来说并不是什么大问题,就像您在磁盘目录中所期望的那样。

答案 1 :(得分:-1)

enter code hereIf Count = 4 Then
        MsgBox("done")
    ElseIf Count = 0
        Dim aryFi As IO.FileInfo() = (di.GetFiles("*.mp4", IO.SearchOption.AllDirectories))
        For Each fi In aryFi
            Dim ico As Icon = Icon.ExtractAssociatedIcon(fi.FullName)
            Dim imagelistsmall As New ImageList()
            Dim item As New ListViewItem(fi.FullName)
            Dim li As ListViewItem
            Try
                li = ListView1.Items.Add(fi.Name, ImageList1.Images.Count)
                li.Tag = fi.FullName
                ImageList1.Images.Add(Bitmap.FromFile(fi.FullName))

                ListView1.LargeImageList = ImageList1
                ListView1.View = View.List
                Me.Controls.Add(ListView1)

            Catch ex As Exception

            End Try
        Next
        Count = (Count.ToString + 1)
        sack()

    ElseIf Count = 1
        Dim aryFi2 As IO.FileInfo() = (di.GetFiles("*.mov", IO.SearchOption.AllDirectories))
        For Each fi In aryFi2

            Dim ico As Icon = Icon.ExtractAssociatedIcon(fi.FullName)
            Dim imagelistsmall As New ImageList()
            Dim item As New ListViewItem(fi.FullName)
            Dim li As ListViewItem
            Try
                li = ListView1.Items.Add(fi.Name, ImageList1.Images.Count)
                li.Tag = fi.FullName
                ImageList1.Images.Add(Bitmap.FromFile(fi.FullName))

                ListView1.LargeImageList = ImageList1
                ListView1.View = View.List
                Me.Controls.Add(ListView1)

            Catch ex As Exception

            End Try
        Next
        Count = (Count.ToString + 1)
        sack()
    ElseIf Count = 2
        Dim aryFi3 As IO.FileInfo() = (di.GetFiles("*.flv", IO.SearchOption.AllDirectories))
        For Each fi In aryFi3

            Dim ico As Icon = Icon.ExtractAssociatedIcon(fi.FullName)
            Dim imagelistsmall As New ImageList()
            Dim item As New ListViewItem(fi.FullName)
            Dim li As ListViewItem
            Try
                li = ListView1.Items.Add(fi.Name, ImageList1.Images.Count)
                li.Tag = fi.FullName
                ImageList1.Images.Add(Bitmap.FromFile(fi.FullName))

                ListView1.LargeImageList = ImageList1
                ListView1.View = View.List
                Me.Controls.Add(ListView1)

            Catch ex As Exception

            End Try
        Next
        Count = (Count.ToString + 1)
        sack()
    ElseIf Count = 3
        Dim aryFi4 As IO.FileInfo() = (di.GetFiles("*.avi", IO.SearchOption.AllDirectories))
        For Each fi In aryFi4

            Dim ico As Icon = Icon.ExtractAssociatedIcon(fi.FullName)
            Dim imagelistsmall As New ImageList()
            Dim item As New ListViewItem(fi.FullName)
            Dim li As ListViewItem
            Try
                li = ListView1.Items.Add(fi.Name, ImageList1.Images.Count)
                li.Tag = fi.FullName
                ImageList1.Images.Add(Bitmap.FromFile(fi.FullName))

                ListView1.LargeImageList = ImageList1
                ListView1.View = View.List
                Me.Controls.Add(ListView1)

            Catch ex As Exception

            End Try
        Next
        Count = (Count.ToString + 1)
        sack()
    End If