Thursday, April 16, 2009

Retrieve All Files Info in the specified Folder (Using LINQ To Object)


        Public Sub getCompleteFileList1(ByVal rootDir As String, ByRef arrFileList As ArrayList) 
            If My.Computer.FileSystem.GetDirectoryInfo(rootDir).Name = "System Volume Information" Then
                Return
            End If

            For Each recursiveDir As String In My.Computer.FileSystem.GetDirectories(rootDir)
                Call getCompleteFileList1(recursiveDir, arrFileList)
            Next

            Dim files = From file In My.Computer.FileSystem.GetFiles(rootDir, FileIO.SearchOption.SearchTopLevelOnly) _
                        Order By file _
                        Select My.Computer.FileSystem.GetFileInfo(file)
            arrFileList.AddRange(files.ToList)

        End Sub

This function return a collection of System.IO.FileInfo objects that can be used to retrieve all files and their corresponding information with same list that can be used depending on the requirement.

If  only the specific attribute is desired for each file, then we can specify the needed attribute under the Select Key

1 comment:

jivangoyal said...

Another method to get all the files deep to the directory based on the filter specified

Public Function getFiles(ByVal curDir As String, ByVal curFilter As String) As ArrayList
Dim arrList As New ArrayList
Dim files = From file In My.Computer.FileSystem.GetFiles(curDir, FileIO.SearchOption.SearchAllSubDirectories) _
Where file.EndsWith(curFilter) _
Order By file _
Select file
arrList.AddRange(files.ToList)
Return arrList
End Function