Condividi tramite


Procedura: Eseguire una query per il file o i file più grandi in un albero di directory (LINQ) (Visual Basic)

Questo esempio mostra cinque query correlate alle dimensioni del file in byte:

  • Come recuperare le dimensioni in byte del file più grande.

  • Come recuperare le dimensioni in byte del file più piccolo.

  • Come recuperare l'oggetto FileInfo più grande o più piccolo file da una o più cartelle in una cartella radice specificata.

  • Come recuperare una sequenza, ad esempio i 10 file più grandi.

  • Come ordinare i file in gruppi in base alle dimensioni del file in byte, ignorando i file con dimensioni inferiori a quelle specificate.

Esempio

L'esempio seguente contiene cinque query separate che illustrano come eseguire query e raggruppare i file, a seconda delle dimensioni del file in byte. È possibile modificare facilmente questi esempi per basare la query su un'altra proprietà dell'oggetto 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  

Per restituire uno o più oggetti completi FileInfo , la query deve prima esaminarne ognuna nell'origine dati e quindi ordinarle in base al valore della relativa proprietà Length. Quindi può restituire il singolo o la sequenza con le lunghezze più grandi. Utilizzare First per restituire il primo elemento di un elenco. Utilizzare Take per restituire il primo numero n di elementi. Specificare un ordinamento decrescente per inserire gli elementi più piccoli all'inizio dell'elenco.

La query richiama un metodo separato per ottenere la dimensione del file in byte al fine di gestire l'eccezione che verrà generata nel caso in cui un file sia stato eliminato in un altro thread dopo che l'oggetto FileInfo è stato creato durante la chiamata a GetFiles. Anche se l'oggetto FileInfo è già stato creato, l'eccezione può verificarsi perché un oggetto FileInfo tenterà di aggiornare la sua proprietà Length utilizzando la dimensione più recente in byte la prima volta che si accede alla proprietà. Inserendo questa operazione in un blocco try-catch all'esterno della query, viene seguita la regola di evitare operazioni nelle query che possono causare effetti collaterali. In generale, è necessario prestare molta attenzione quando si utilizzano eccezioni, per assicurarsi che un'applicazione non venga lasciata in uno stato sconosciuto.

Compilare il codice

Creare un progetto di applicazione console di Visual Basic con un'istruzione Imports per lo spazio dei nomi System.Linq.

Vedere anche