此範例顯示五個與檔案大小相關的查詢,以位元組為單位:
如何擷取最大檔案位元組的大小。
如何取得最小檔案的位元組大小。
如何從指定根資料夾下的一或多個資料夾擷取 FileInfo 物件最大或最小檔案。
如何擷取序列,例如10個最大的檔案。
如何根據檔案大小以位元組為單位將檔案排序為群組,忽略小於指定大小的檔案。
範例
下列範例包含五個不同的查詢,顯示如何根據檔案大小以位元組為單位來查詢和群組檔案。 您可以輕易地修改這些範例,以根據物件的一些其他屬性 FileInfo 進行查詢。
Module QueryBySize
Sub Main()
' Change the drive\path if necessary
Dim root As String = "C:\Program Files\Microsoft Visual Studio 9.0"
'Take a snapshot of the folder contents
Dim dir As New System.IO.DirectoryInfo(root)
Dim fileList = dir.GetFiles("*.*", System.IO.SearchOption.AllDirectories)
' Return the size of the largest file
Dim maxSize = Aggregate aFile In fileList Into Max(GetFileLength(aFile))
'Dim maxSize = fileLengths.Max
Console.WriteLine("The length of the largest file under {0} is {1}", _
root, maxSize)
' Return the FileInfo object of the largest file
' by sorting and selecting from the beginning of the list
Dim filesByLengDesc = From file In fileList _
Let filelength = GetFileLength(file) _
Where filelength > 0 _
Order By filelength Descending _
Select file
Dim longestFile = filesByLengDesc.First
Console.WriteLine("The largest file under {0} is {1} with a length of {2} bytes", _
root, longestFile.FullName, longestFile.Length)
Dim smallestFile = filesByLengDesc.Last
Console.WriteLine("The smallest file under {0} is {1} with a length of {2} bytes", _
root, smallestFile.FullName, smallestFile.Length)
' Return the FileInfos for the 10 largest files
' Based on a previous query, but nothing is executed
' until the For Each statement below.
Dim tenLargest = From file In filesByLengDesc Take 10
Console.WriteLine("The 10 largest files under {0} are:", root)
For Each fi As System.IO.FileInfo In tenLargest
Console.WriteLine("{0}: {1} bytes", fi.FullName, fi.Length)
Next
' Group files according to their size,
' leaving out the ones under 200K
Dim sizeGroups = From file As System.IO.FileInfo In fileList _
Where file.Length > 0 _
Let groupLength = file.Length / 100000 _
Group file By groupLength Into fileGroup = Group _
Where groupLength >= 2 _
Order By groupLength Descending
For Each group In sizeGroups
Console.WriteLine(group.groupLength + "00000")
For Each item As System.IO.FileInfo In group.fileGroup
Console.WriteLine(" {0}: {1}", item.Name, item.Length)
Next
Next
' Keep the console window open in debug mode
Console.WriteLine("Press any key to exit.")
Console.ReadKey()
End Sub
' This method is used to catch the possible exception
' that can be raised when accessing the FileInfo.Length property.
' In this particular case, it is safe to ignore the exception.
Function GetFileLength(ByVal fi As System.IO.FileInfo) As Long
Dim retval As Long
Try
retval = fi.Length
Catch ex As FileNotFoundException
' If a file is no longer present,
' just return zero bytes.
retval = 0
End Try
Return retval
End Function
End Module
若要傳回一或多個完整的 FileInfo 對象,查詢必須先檢查數據源中的每個物件,然後依其 Length 屬性的值加以排序。 然後,它可以傳回長度最大的單一或序列。 使用 First 傳回清單中的第一個元素。 使用 Take 傳回前 n 個元素數目。 指定遞減排序順序,將最小的元素放在清單開頭。
查詢呼叫了另一個方法,以位元組為單位獲取檔案大小,來處理可能出現的例外狀況。當物件是在FileInfo中建立後在接下來的期間,檔案於GetFiles上在另一個執行緒上被刪除時,這可能會引發例外狀況。 即使 FileInfo 物件已經建立,仍可能發生例外狀況,因為 FileInfo 物件在第一次存取屬性時,會嘗試使用目前的位元組大小來重新整理其 Length 屬性。 藉由將這個作業放在查詢外部的 try-catch 區塊中,我們遵循了避免在查詢中進行可能導致副作用之操作的規則。 一般而言,取用例外狀況時必須非常小心,以確保應用程式不會處於未知狀態。
編譯程式碼
建立一個包含 Imports 語句以使用 System.Linq 命名空間的 Visual Basic 控制台應用程式專案。