Thursday, April 16, 2009

Filling the DataGridView with all the files(upto deepest level) in specified directory

In case we need to fill a DataGridView in our form with all the files in the specified directory, no matter how deep many files exists, all the files are retireved from the directory and the related information is displayed in the DataGridView.

The steps to get the result as below:
1. Get the collection of FileInfo for each file in the directory/drive by using function explained in "Retrieve All Files Info in the specified Folder (Using LINQ To Object)" blog
2. Add the DataGridView component in the form name it anything say DataGridView1
3. Add the following string in the action corresponding to which data grid is needed to be filled with the file information:

       Dim arrList as New ArrayList
       getCompleteFileList1(directoryName, arrList)
       dataGridView1.DataSource= arrList

Or If only specific list of attributes for each file is needed to be displayed the the following lines can be considered:

       Dim arrList As New ArrayList
       getCompleteFileList1(directoryName, arrList)
        Dim filesInfo = From file As IO.FileInfo In arrList _
                       Select file.Name, file.DirectoryName, file.Length, file.LastWriteTime
        dataGridView1.DataSource = filesInfo.ToList


No comments: