0

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

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

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

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

4

2 回答 2

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,就像您在磁盘目录上所期望的那样。

于 2013-09-01T13:23:10.277 回答
-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
于 2016-06-06T18:56:34.377 回答