2013-09-01 76 views
0

我必須得到一個目錄文件列表,並根據多個擴展名進行過濾......然後排序!使用具有多個擴展名的Directory.GetFiles()和排序順序

我用這個,這是我發現得到過濾,多臺分機目錄內容的最快方法:

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

,然後使用數組排序。

我想知道(這是我的問題;))如果有一種方法可以在同一條主線上進行排序嗎? A類:

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

,如果是的,如果我想獲得速度這樣做,而不是在最後排數組(但我會很快做我的測試和report..as我得到一個解決方案! !)? 感謝您的幫助!

回答

1

您可以使用OrderBy()LINQ的擴展方法,像這樣:

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

它不會讓對速度有什麼區別,分類本身是O(n日誌(n))的複雜性。它確實在存儲中產生差異,OrderBy()具有O(n)存儲要求。 Array.Sort()就地排序。對於小型n值不是什麼大問題,就像您對磁盤目錄所期望的那樣。

0
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 
相關問題