Question Show All Files Folders and Subfiles and Subfolders

bagwis

Member
Joined
Apr 18, 2012
Messages
8
Programming Experience
1-3
Good day, I need some assistance here, I am trying to create my own simple file explorer, in my form I have a Listview and a Combobox and I have these codes below.
In my form load I have this code to list in comobobox all the existing folders in my current directory.
            Dim dir As New DirectoryInfo(Environment.CurrentDirectory & "\")
Dim dirarr As DirectoryInfo() = dir.GetDirectories()
            Dim directinfo As DirectoryInfo
            For Each directinfo In dirarr
                ListView1.Items.Add(directinfo.Name)
            Next directinfo


While in my combobox selectedindex event I have this
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
If ListView1.Items.Count = 0 Then
            filesinfolder()
        Else
            ListView1.Items.Clear()
            filesinfolder()
        End If
    End Sub


I have a sub called filesinfolder
Sub filesinfolder()
        Try
            Dim mb As Double = 1048576 + 1
            Dim di As New IO.DirectoryInfo(Environment.CurrentDirectory & "\" & ComboBox1.Text)
            Dim aryFi As IO.FileInfo() = di.GetFiles("*")
            Dim fi As IO.FileInfo
            Dim lv As ListViewItem
            Dim filesize As Double
            Dim fileformat As Double


            Dim dir As New DirectoryInfo(Environment.CurrentDirectory & "\" & Combobox1.Text)
            Dim dirarr As DirectoryInfo() = dir.GetDirectories()
            Dim directinfo As DirectoryInfo
            For Each directinfo In diArr
                ListView1.Items.Add(directinfo.Name)
            Next directinfo
            
            For Each fi In aryFi
                filesize = fi.Length / mb
                fileformat = Format(filesize, "#,##0.00")
                lv = ListView1.Items.Add(fi.Name)
                lv.SubItems.Add(fileformat)
                lv.SubItems.Add(fi.Extension)
            Next


        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub


The sub above list all the files and subfolders that is selected from the combobox, but what I want is to access the files in subfolders and sub-subfolders and so on by simply double clicking the folder name. How can I possibly do that? I tried to include the directoryinfo in listview doubleclick event but it only gives me an error that it is inaccessible. All works fine and I can open the files but I cannot access files in subfolders and so on. Or how can I include the subfolders and other folders inside a subfolder (if any) in my combobox? So that when I select a folder name all files beneath will be shown in my listview. Any idea? Thanks in advance
 
Last edited by a moderator:
Still no reply? Additionally, I have this code in my listview1 double click event
 Private Sub ListView1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDoubleClick
        With ListView1.SelectedItems(0)
            txtFileName.Text = .SubItems(0).Text
        End With
    End Sub

But when I tried to add this
Dim dir As New DirectoryInfo(Environment.CurrentDirectory & "\" & Combobox1.Text)
Dim dirarr As DirectoryInfo() = dir.GetDirectories()
Dim directinfo As DirectoryInfo
For Each directinfo In diArr
ListView1.Items.Add(directinfo.Name)
Next directinfo

It gives me an error that the directory does not exist. How can I resolve this one? Thank you

 
Have you considered using a TreeView on the left side for the folders and sub folders, then on the right side the ListView for the files in the selected folder on the TreeView?
 
Thanks for the reply, I already know this code, what I am trying to do is that list all files and folders as well as all subfolders sub-subfolders (if any) and files in it and so on and display it on my a listview then if the user will double click on the filename it will open the file whether it is a document, excel, exe and others. I am making a trial and error, hoping that I could be able to solve this, I have this code here.
Dim FileName As String
Dim FilePath As String


If ListView1.SelectedItems.Count > 0 Then
FileName = ListView1.SelectedItems(0).Text
FilePath = Environment.CurrentDirectory & TreeView1.ToString & "\" & strFileName
Process.Start(strPath)
End If


But this returns an error saying that "it cannot find the specified file" while if I remove the TreeView1.ToString it will open a file BUT only on the current folder if I go under a subfolder or sub-subfolder it will give me the same error. Any idea? :D
 
This will populate a ListView with all the files under a particular folder, with the file name in the first column and the folder path in the second. It will then execute that file when the item is double-clicked:
Private Sub PopulateListView(folderPath As String)
    Dim filePaths = GetAllFilePaths(folderPath)
    Dim items = filePaths.Select(Function(filePath) New ListViewItem(Path.GetFileName(filePath),
                                                                     Path.GetDirectoryName(filePath)))

    ListView1.Items.AddRange(items.ToArray())
End Sub

Private Function GetAllFilePaths(folderPath As String) As List(Of String)
    Dim filePaths As New List(Of String)

    Try
        filePaths.AddRange(Directory.GetFiles(folderPath))

        For Each subfolderPath In Directory.GetDirectories(folderPath)
            filePaths.AddRange(GetAllFilePaths(subfolderPath))
        Next
    Catch
    End Try

    Return filePaths
End Function

Private Sub ListView1_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles ListView1.MouseDoubleClick
    Dim item = ListView1.GetItemAt(e.X, e.Y)

    If item IsNot Nothing Then
        Dim filepath = Path.Combine(item.SubItems(1).Text, item.Text)

        Process.Start(filepath)
    End If
End Sub
Note that that code is not production quality. It needs some validation and exception handling, e.g. if a file is deleted after being scanned or there is no handler for the file type.
 
Thanks sir, will try that one :D Though I was able to populate all the folders in a treeview and show files in it on my listview but I'm getting hard time to think on how to execute files. :D

EDIT:
Already tried the code but I got this error on line 3 of your post
VB.NET:
'Select' is not a member of 'System.Collections.Generic.List(Of String)'
 
Last edited:
If you're using .NET 3.5 or later and you have the appropriate references and imports, which a WinForms application should by default, then it will work as is. You a reference to System.Core.dll and you need to import System.Linq, both of which should be done by default, so I can only assume that either you're not using .NET 4.0, as you profile suggests, or you have removed one or the other of those.
 
Back
Top