CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    VB.NET 2005 General : How do I make my own Windows Explorer Application?

    Q: How do I list All Folders & SubFolders in a Treeview?

    A:
    • Import the IO Namespace. Add the following Above your form's Class declaration:
      Code:
      Imports System.IO
    • Add the Following Sub under the Private Class statement.
      Code:
          Private Sub AddAllFolders(ByVal TNode As TreeNode, ByVal FolderPath As String)
              Try
                  For Each FolderNode As String In Directory.GetDirectories(FolderPath)
                      Dim SubFolderNode As TreeNode = TNode.Nodes.Add(FolderNode.Substring(FolderNode.LastIndexOf("\"c) + 1))
      
                      SubFolderNode.Tag = FolderNode
      
                      SubFolderNode.Nodes.Add("Loading...")
                  Next
              Catch ex As Exception
                  MsgBox(ex.Message)
              End Try
          End Sub
    • In Form Load, or any other event, call the above created sub
      Code:
          Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      
              Dim Tnode As TreeNode = TreeView1.Nodes.Add("(Drive C:)")
              AddAllFolders(Tnode, "C:\")
           End Sub
    • In the Treeview_AfterSelect event, enable the capability to show the sub folders, again, by calling the AddAllFolders Sub :
      Code:
          Private Sub Treeview1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
              If TreeView1.SelectedNode.Nodes.Count = 1 AndAlso TreeView1.SelectedNode.Nodes(0).Text = "Loading..." Then
      
                  TreeView1.SelectedNode.Nodes.Clear()
      
                  AddAllFolders(TreeView1.SelectedNode, CStr(TreeView1.SelectedNode.Tag))
      
              End If
          End Sub
    • Do the same with the Treeview_BeforeExpand event :
      Code:
          Private Sub Treeview1_BeforeExpand(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeExpand
      
              If e.Node.Nodes.Count = 1 AndAlso e.Node.Nodes(0).Text = "Loading..." Then
      
                  e.Node.Nodes.Clear()
                  AddAllFolders(e.Node, CStr(e.Node.Tag))
      
              End If
      
          End Sub


    If you were to run this code now, your Treeview will show all the Folders and all the Subfolders within each folder, and all Sub folders within those etc..

    Q: How do I list all the files within each folder also in the Treeview?

    A: To list all the folders' files as well, simply add the following to the Treeview_AfterSelect event. Add this under the already existing If/End If structure:
    Code:
            Dim folder As String = CStr(e.Node.Tag)
    
            If Not folder Is Nothing AndAlso IO.Directory.Exists(folder) Then
                Try
                    For Each file As String In IO.Directory.GetFiles(folder)
                        e.Node.Nodes.Add(file.Substring(file.LastIndexOf("\"c) + 1))
    
                    Next
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
    When a node is selected, the file names will also appear as sub nodes under the current selected node.

    Q: What if I don't want the Filenames to appear in the Treeview, but in a ListView instead, what to do then?

    A:
    • Add a ListView to your form, and name it. (with this FAQ, I kept the default names)
    • Remove the previous code (the code that added the Files) from the TreeView_AfterSelect event.
    • In Form_Load, add the following code, in order for us to have columns in our ListView:
      Code:
              ListView1.View = View.Details
              ' Add a column with width 80 and left alignment
              ListView1.Columns.Add("File Name", 150, HorizontalAlignment.Left)
              ListView1.Columns.Add("File Type", 80, HorizontalAlignment.Left)
              ListView1.Columns.Add("Date Modified", 150, HorizontalAlignment.Left)
      Add the above code just underneath the already existing code, i.e, just above End Sub
    • Add the following code to the TreeView_AfterSelect event, to show the Folders' contents in the ListView. The code is almost exactly the same as our previous code in this event (The code to load the files into the Treeview)
      Code:
              Dim folder As String = CStr(TreeView1.SelectedNode.Tag)
      
              If Not folder Is Nothing AndAlso IO.Directory.Exists(folder) Then
                  Try
                      For Each file As String In IO.Directory.GetFiles(folder)
                          ListView1.Items.Add(file.Substring(file.LastIndexOf("\"c) + 1), file.ToString())
                      Next
                  Catch ex As Exception
                      MsgBox(ex.Message)
                  End Try
              End If

    This will load all the files in the selected folder (selected node) into the ListView

    Q: How do I add File Details such as The File Extension, and the Last Modified Date in the ListView?

    A:
    • In the TreeView_AfterSelect event, declare the following 3(three) variables, just underneath the Private Sub statement:
      Code:
              Dim FileExtension As String
              Dim SubItemIndex As Integer
              Dim DateMod As String
    • In The Try catch block, just underneath the For Each block, add the following, to get the File Extension, and get the Last Modified date:
      Code:
                          FileExtension = IO.Path.GetExtension(file)
                          DateMod = IO.File.GetLastWriteTime(file).ToString()
    • Just after the statement where we added the File Name to the ListView, add these as well:
      Code:
                          ListView1.Items(SubItemIndex).SubItems.Add(FileExtension.ToString() & " File")
                          ListView1.Items(SubItemIndex).SubItems.Add(DateMod.ToString())
                          SubItemIndex += 1

    When run, this will display all the selected Folder's files with their File Extensions, and Last Modified Dates.

    Q: How would I display the Associated File Icons too, in the ListView?

    A:
    • Add an ImageList to your form (Found in All Windows Forms tab of the Toolbox), keep the default name
    • Set the SmallImageList property of the ListView, to the name of your ImageList (ImageList1, in my case)
    • Add the Following Import to our code, because we are going to use APIs to retrieve this information :
      Code:
      Imports System.Runtime.InteropServices
    • Add the Following structure, API function and API cconstants, just above our AddAllFolder sub:
      Code:
          Private Structure SHFILEINFO
              Public hIcon As IntPtr            ' : icon
              Public iIcon As Integer           ' : icondex
              Public dwAttributes As Integer    ' : SFGAO_ flags
               _
              Public szDisplayName As String
               _
              Public szTypeName As String
          End Structure
      
          Private Declare Auto Function SHGetFileInfo Lib "shell32.dll" _
                  (ByVal pszPath As String, _
                   ByVal dwFileAttributes As Integer, _
                   ByRef psfi As SHFILEINFO, _
                   ByVal cbFileInfo As Integer, _
                   ByVal uFlags As Integer) As IntPtr
      
          Private Const SHGFI_ICON = &H100
          Private Const SHGFI_SMALLICON = &H1
          Private Const SHGFI_LARGEICON = &H0    ' Large icon
          Private Const MAX_PATH = 260
    • Add our own sub to extract the File Icons, and store the found icon into the ImageList:
      Code:
          Private Sub AddImages(ByVal strFileName As String)
      
              Dim shInfo As SHFILEINFO
              shInfo = New SHFILEINFO()
              shInfo.szDisplayName = New String(vbNullChar, MAX_PATH)
              shInfo.szTypeName = New String(vbNullChar, 80)
              Dim hIcon As IntPtr
              hIcon = SHGetFileInfo(strFileName, 0, shInfo, Marshal.SizeOf(shInfo), SHGFI_ICON Or SHGFI_SMALLICON)
              Dim MyIcon As Drawing.Bitmap
              MyIcon = Drawing.Icon.FromHandle(shInfo.hIcon).ToBitmap
              ImageList1.Images.Add(strFileName.ToString(), MyIcon)
      
          End Sub
    • Add the Call to the AddImages function, in the TreeView_AfterSelect event, just underneath DateMod = IO.File.GetLastWriteTime(file).ToString()


    When run, After a Folder is selected, you will see all files, with their Associated File Icons, and the previous details in the ListView

    Q: How do I clear the Old Items from a Listview?

    A: Simply add the following statement in the Treeview_AfterSelect event, just underneath Dim DateMod As String
    Code:
            ListView1.Items.Clear()
    Q: How do I Sort the Treeview, in order to show all items in the Treeview alphabetically

    A:Simply add the following statement in Form_Load
    Code:
            TreeView1.Sort()
    A sample is also attached in this FAQ
    Attached Files Attached Files
    Last edited by HanneSThEGreaT; March 15th, 2007 at 05:14 AM.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured