vb.net 使用具有多个扩展名和排序顺序的 Directory.GetFiles()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18557425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:53:36  来源:igfitidea点击:

Using Directory.GetFiles() WITH multiple extensions AND sort order

vb.netsortinggetfiles

提问by Jayme

I have to get a directory file list, filtered on multiple extensions...and sorted!

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

I use this, which is the fastest way I've found to get dir content filtered on multiple extensions:

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

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

and then use an array sort.

然后使用数组排序。

I was wondering (and this is my question ;)) if there would be a way to do the sorting IN the same main line? A kind of:

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

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

and, if yes, if I would gain speed doing this instead of sorting the array at the end (but I would do my test and report..as soon as I get a solution!!)? Thanks for your help!!

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

回答by Hans Passant

You can use the OrderBy() Linq extension method, like this:

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

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

It won't make any difference for speed, sorting is inherently O(nlog(n)) complexity. It doesmake a diffence in storage, OrderBy() has O(n) storage requirement. Array.Sort() sorts in-place. Not a big deal for small nvalues, like you'd expect on a disk directory.

它不会对速度产生任何影响,排序本质上是 O(nlog(n)) 复杂度。它确实在存储上有所不同,OrderBy() 具有 O(n) 存储要求。Array.Sort() 就地排序。对于小n值来说没什么大不了的,就像您对磁盘目录所期望的那样。

回答by Noli

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