Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By jmcilhinney

Thread: Show All Files Folders and Subfiles and Subfolders

  1. #1
    bagwis is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2012
    Posts
    8
    Reputation
    0

    Show All Files Folders and Subfiles and Subfolders

    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 jmcilhinney; 06-02-2012 at 12:28 AM. Reason: Changed xcode options to "vb" so cod eis formatted correctly

  2. #2
    bagwis is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2012
    Posts
    8
    Reputation
    0
    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


  3. #3
    JuggaloBrotha's Avatar
    JuggaloBrotha is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    Lansing, MI; USA
    Posts
    4,321
    Reputation
    959
    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?
    Currently using: VS 2010 Ultimate on Win7 Ultimate x64.


  4. #4
    bagwis is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2012
    Posts
    8
    Reputation
    0
    I have solved this problem, now I am thinking on how to open any file within the directory and subdirectories.

  5. #5
    JuggaloBrotha's Avatar
    JuggaloBrotha is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    Lansing, MI; USA
    Posts
    4,321
    Reputation
    959
    System.Diagnostics.Process.Start("<File path>")
    Currently using: VS 2010 Ultimate on Win7 Ultimate x64.


  6. #6
    bagwis is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2012
    Posts
    8
    Reputation
    0
    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?

  7. #7
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    11,333
    Reputation
    1543
    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.
    bagwis likes this.

  8. #8
    bagwis is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2012
    Posts
    8
    Reputation
    0
    Thanks sir, will try that one 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.

    EDIT:
    Already tried the code but I got this error on line 3 of your post
    Code:
    'Select' is not a member of 'System.Collections.Generic.List(Of String)'
    Last edited by bagwis; 06-09-2012 at 8:54 AM.

  9. #9
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    11,333
    Reputation
    1543
    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking