Add logical drives in a browse treeview...

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 1197 Views - Last Post: 13 January 2017 - 01:03 AM Rate Topic: -----

#1 Dreamfall  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 107
  • Joined: 26-January 15

Add logical drives in a browse treeview...

Posted 07 January 2017 - 09:10 AM

Guys I should add the logical drives in this code, I tried in different ways but it overlaps all, I can only get the physical drives, someone can help me .... :helpsmilie:/>

Private MyRootPath As String = "C:\"
    Property RootPath() As String
        Get
            Return MyRootPath
        End Get
        Set(ByVal Value As String)
            MyRootPath = Value
        End Set
    End Property

    Dim RootNode As New TreeNode
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim Drives As Collections.ObjectModel.ReadOnlyCollection(Of IO.DriveInfo) = My.Computer.FileSystem.Drives
        Me.BrowseTreeViewOne.Nodes.Clear()
        For I As Integer = 0 To Drives.Count - 1
            If Not (Drives(I).IsReady) Then
                Continue For
            End If
            RootNode = New TreeNode(Drives(I).Name.ToString)
            RootNode.Tag = Drives(I).Name.ToString
            RootNode.Nodes.Add("*DUMMY*")
            Me.BrowseTreeViewOne.Nodes.Add(RootNode)
            RootNode.ImageKey = CacheShellIcon(Drives(I).Name)
            RootNode.SelectedImageKey = RootNode.ImageKey
        Next
    End Sub

    Function CacheShellIcon(ByVal ArgPath As String) As String
        Dim Icon_Key As String = Nothing
        If (IO.Directory.Exists(ArgPath)) = True Then
            If ArgPath.Trim.Length = 3 Then
                Icon_Key = "DRIVE"
            Else
                Icon_Key = "FOLDER"
            End If
        ElseIf (IO.File.Exists(ArgPath)) = True Then
            Icon_Key = IO.Path.GetExtension(ArgPath)
        End If
        If (ImageIcon.Images.ContainsKey(Icon_Key)) = False Then
            ImageIcon.Images.Add(Icon_Key, GetShellIconAsImage(ArgPath))
        End If
        Return Icon_Key
    End Function
    Private Sub BrowseTreeViewOne_BeforeCollapse(ByVal sender As Object, ByVal e As TreeViewCancelEventArgs) Handles BrowseTreeViewOne.BeforeCollapse
        e.Node.Nodes.Clear()
        e.Node.Nodes.Add("*DUMMY*")
    End Sub

    Private Sub BrowseTreeViewOne_BeforeExpand(ByVal sender As Object, ByVal e As TreeViewCancelEventArgs) Handles BrowseTreeViewOne.BeforeExpand
        Try
            e.Node.Nodes.Clear()
            Dim NodeDirectory As IO.DirectoryInfo
            NodeDirectory = New IO.DirectoryInfo(e.Node.Tag.ToString)
            For Each Directory As IO.DirectoryInfo In NodeDirectory.GetDirectories
                Dim DirectoryNode As New TreeNode
                DirectoryNode.Tag = Directory.FullName
                DirectoryNode.Text = Directory.Name
                DirectoryNode.Nodes.Add("*DUMMY*")
                DirectoryNode.ImageKey = CacheShellIcon(Directory.FullName)
                DirectoryNode.SelectedImageKey = DirectoryNode.ImageKey & "-open"
                e.Node.Nodes.Add(DirectoryNode)
            Next
            For Each File As IO.FileInfo In NodeDirectory.GetFiles
                Dim FileNode As New TreeNode
                FileNode.Tag = File.FullName
                FileNode.Text = File.Name
                FileNode.ImageKey = CacheShellIcon(File.FullName)
                FileNode.SelectedImageKey = FileNode.ImageKey & "-open"
                e.Node.Nodes.Add(FileNode)
            Next
            'Dim sDrive As String, sDrives() As String
            'sDrives = ListAllDrives()
            'For Each sDrive In sDrives
            '    Me.BrowseTreeViewOne.Nodes.Add(sDrive)
            'Next
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
        End Try
    End Sub

    'Public Function ListAllDrives() As String()
    '    Dim arDrives() As String
    '    arDrives = Directory.GetLogicalDrives()
    '    Return arDrives
    'End Function



:rolleyes2:/>

Is This A Good Question/Topic? 0
  • +

Replies To: Add logical drives in a browse treeview...

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13047
  • View blog
  • Posts: 51,615
  • Joined: 12-June 08

Re: Add logical drives in a browse treeview...

Posted 07 January 2017 - 09:21 AM

Quote

I tried in different ways but it overlaps all,

Huh?
Was This Post Helpful? 0
  • +
  • -

#3 Dreamfall  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 107
  • Joined: 26-January 15

Re: Add logical drives in a browse treeview...

Posted 07 January 2017 - 09:51 AM

I this result and the logical and physical drives are repeated several times, if I expand and collapse the main nodes are repeated with more duplication .... <_</>

Posted Image
Was This Post Helpful? 0
  • +
  • -

#4 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1413
  • View blog
  • Posts: 3,614
  • Joined: 01-February 13

Re: Add logical drives in a browse treeview...

Posted 07 January 2017 - 10:47 AM

I don`t seem to be able to reproduce the problem you are having. Try testing the code below (from your code) in a new test project. Just add 1 Treeview to the form and run it to see if it still adds doubles.

Also, are any of your physical drives partitioned up into multiple logical drives?

Public Class Form1
    Dim RootNode As New TreeNode

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim Drives As Collections.ObjectModel.ReadOnlyCollection(Of IO.DriveInfo) = My.Computer.FileSystem.Drives
        For I As Integer = 0 To Drives.Count - 1
            If (Drives(I).IsReady) Then
                RootNode = New TreeNode(Drives(I).Name.ToString)
                RootNode.Tag = Drives(I).Name.ToString
                RootNode.Nodes.Add("*DUMMY*")
                Me.BrowseTreeViewOne.Nodes.Add(RootNode)
            End If
        Next
    End Sub

    Private Sub BrowseTreeViewOne_BeforeCollapse(ByVal sender As Object, ByVal e As TreeViewCancelEventArgs) Handles BrowseTreeViewOne.BeforeCollapse
        e.Node.Nodes.Clear()
        e.Node.Nodes.Add("*DUMMY*")
    End Sub

    Private Sub BrowseTreeViewOne_BeforeExpand(ByVal sender As Object, ByVal e As TreeViewCancelEventArgs) Handles BrowseTreeViewOne.BeforeExpand
        Try
            e.Node.Nodes.Clear()
            Dim NodeDirectory As IO.DirectoryInfo
            NodeDirectory = New IO.DirectoryInfo(e.Node.Tag.ToString)
            For Each Directory As IO.DirectoryInfo In NodeDirectory.GetDirectories
                Dim DirectoryNode As New TreeNode
                DirectoryNode.Tag = Directory.FullName
                DirectoryNode.Text = Directory.Name
                DirectoryNode.Nodes.Add("*DUMMY*")
                e.Node.Nodes.Add(DirectoryNode)
            Next
            For Each File As IO.FileInfo In NodeDirectory.GetFiles
                Dim FileNode As New TreeNode
                FileNode.Tag = File.FullName
                FileNode.Text = File.Name
                e.Node.Nodes.Add(FileNode)
            Next
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
        End Try
    End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

#5 Dreamfall  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 107
  • Joined: 26-January 15

Re: Add logical drives in a browse treeview...

Posted 07 January 2017 - 11:17 AM

Hi Razer ... This way you only see the physical units, the two HD (C:\ & E:\), if I insert a CD into the logic that show all ok. but I need all units are visible, also the virtual drives that they are not in operation. :dozingoff:

Posted Image

This post has been edited by Dreamfall: 07 January 2017 - 11:19 AM

Was This Post Helpful? 0
  • +
  • -

#6 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1413
  • View blog
  • Posts: 3,614
  • Joined: 01-February 13

Re: Add logical drives in a browse treeview...

Posted 07 January 2017 - 12:26 PM

Well, i copied the code you posted that you said was creating the doubles so, apparently you are using different code that is listing all the doubles. You did not show or explain the code that is causing that. So, i am only going by the code you showed.

Perhaps change these two subs in that last example and see if it lists all of them then. The first is the Load event where i commented out your If Then that checks if the drive is ready. If a drive is not ready, you can still get its name.
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim Drives As Collections.ObjectModel.ReadOnlyCollection(Of IO.DriveInfo) = My.Computer.FileSystem.Drives
        For I As Integer = 0 To Drives.Count - 1
            'If (Drives(I).IsReady) Then
            RootNode = New TreeNode(Drives(I).Name.ToString)
            RootNode.Tag = Drives(I).Name.ToString
            RootNode.Nodes.Add("*DUMMY*")
            Me.BrowseTreeViewOne.Nodes.Add(RootNode)
            'End If
        Next
    End Sub



However, if a drive is not ready, you would want to stop the code from trying to access any files on it. So, cancel the Node from expanding if they try expanding a drive that is not ready.
    Private Sub BrowseTreeViewOne_BeforeExpand(ByVal sender As Object, ByVal e As TreeViewCancelEventArgs) Handles BrowseTreeViewOne.BeforeExpand
        Try
            'Get the ready state of the drive this Node`s Tag is set to
            Dim Ready As Boolean = (From d As IO.DriveInfo In IO.DriveInfo.GetDrives Where d.Name = e.Node.Tag.ToString Select d.IsReady).FirstOrDefault

            If Ready Then
                e.Node.Nodes.Clear()
                Dim NodeDirectory As IO.DirectoryInfo
                NodeDirectory = New IO.DirectoryInfo(e.Node.Tag.ToString)
                For Each Directory As IO.DirectoryInfo In NodeDirectory.GetDirectories
                    Dim DirectoryNode As New TreeNode
                    DirectoryNode.Tag = Directory.FullName
                    DirectoryNode.Text = Directory.Name
                    DirectoryNode.Nodes.Add("*DUMMY*")
                    e.Node.Nodes.Add(DirectoryNode)
                Next
                For Each File As IO.FileInfo In NodeDirectory.GetFiles
                    Dim FileNode As New TreeNode
                    FileNode.Tag = File.FullName
                    FileNode.Text = File.Name
                    e.Node.Nodes.Add(FileNode)
                Next

            Else
                e.Cancel = True
                MessageBox.Show("Drive is not ready. It must have a disc in it before you can navigate it.")
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
        End Try
    End Sub



Other than that, i still don`t know what code you where executing to get doubles. The code i changed above will list my DVD-ROM but, will not let me expand the node in the TreeView unless i have a CD or DVD inserted in it.
Was This Post Helpful? 1
  • +
  • -

#7 Dreamfall  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 107
  • Joined: 26-January 15

Re: Add logical drives in a browse treeview...

Posted 07 January 2017 - 01:26 PM

Hoo perfect Razer, in the end only had to remove this code...

If Not (Drives(I).IsReady) Then
   Continue For
End If



and everything works as I wanted..:-)

Razer one last thing if you have time and you do not create too much trouble, you can add the "DESKTOP" folder at the top of the tree, to reach it faster.

Desktop
-**Drives "A" through "Z"**
-**All Folders On Desktop**



Thanks again Razer.

This post has been edited by Dreamfall: 07 January 2017 - 01:27 PM

Was This Post Helpful? 0
  • +
  • -

#8 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1413
  • View blog
  • Posts: 3,614
  • Joined: 01-February 13

Re: Add logical drives in a browse treeview...

Posted 07 January 2017 - 02:16 PM

Doing the Desktop is a bit more work than the standard drives and folders. That is because, there are two folders that you would need to get the files from for the desktop. If a program is installed on the computer for all users, the shortcut for the desktop is placed in the CommonDesktopDirectory which is located at "C:\Users\Public\Desktop". However, programs that are installed for only the current user would be put in the DesktopDirectory or Desktop which would be located at "C:\Users\CurrentUserName\Desktop".

You can get both paths by using the Environment SpecialFolders as shown here.
        Dim PublicDesktopFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory)
        Dim CurrentUserDesktopFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)



So, you would have to add the Desktop Node to the Treeview and when it is expanded, you would have to get the files/folders from both of those directories to add as child nodes.
Was This Post Helpful? 0
  • +
  • -

#9 Dreamfall  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 107
  • Joined: 26-January 15

Re: Add logical drives in a browse treeview...

Posted 07 January 2017 - 02:42 PM

I tried but it does not work, I have the node but not expand anything :unsure:
Dim CurrentUserDesktopFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
        Me.BrowseTreeViewOne.Nodes.Add(CurrentUserDesktopFolder)


Was This Post Helpful? 0
  • +
  • -

#10 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1413
  • View blog
  • Posts: 3,614
  • Joined: 01-February 13

Re: Add logical drives in a browse treeview...

Posted 08 January 2017 - 09:21 PM

I have submitted a tutorial here on DIC today that covers all this. You can read through it and see if it helps you figure it out. It is at the link below.

TreeView Drive, Folder, and File Explorer
Was This Post Helpful? 1
  • +
  • -

#11 Dreamfall  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 107
  • Joined: 26-January 15

Re: Add logical drives in a browse treeview...

Posted 09 January 2017 - 04:50 AM

I already solved by adding this function to the code that I have Posted...

 Me.Load ....
        Me.Create_Documents(BrowseTreeViewOne)
        Me.Create_Desktop(BrowseTreeViewOne)
 End Sub



Dim Documents As New TreeNode()
    Public Function Create_Documents(TreeView As TreeView) As Boolean
        Dim Return_Value As Boolean = False
        Try
            ' Create Documents
            Documents.Text = "My Documents"
            Documents.Tag = FileIO.SpecialDirectories.MyDocuments
            Documents.Nodes.Add("")
            TreeView.Nodes.Add(Documents)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            Return_Value = False
        End Try
        Return Return_Value
    End Function

    Dim Desktop As New TreeNode()
    Public Function Create_Desktop(TreeView As TreeView) As Boolean
        Dim Return_Value As Boolean = False
        Try
            ' Create Desktop
            Desktop.Text = "My Desktop"
            Desktop.Tag = FileIO.SpecialDirectories.Desktop
            Desktop.Nodes.Add("")
            TreeView.Nodes.Add(Desktop)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            Return_Value = False
        End Try
        Return Return_Value
    End Function

ETC......



Private Sub BrowseTreeViewOne_BeforeExpand(ByVal sender As Object, ByVal e As TreeViewCancelEventArgs) Handles BrowseTreeViewOne.BeforeExpand
        Try
            e.Node.Nodes.Clear()
            Dim NodeDirectory As IO.DirectoryInfo
            NodeDirectory = New IO.DirectoryInfo(e.Node.Tag.ToString)
            For Each Directory As IO.DirectoryInfo In NodeDirectory.GetDirectories
                Dim DirectoryNode As New TreeNode
                DirectoryNode.Tag = Directory.FullName
                DirectoryNode.Text = Directory.Name
                DirectoryNode.Nodes.Add("*DUMMY*")
                e.Node.Nodes.Add(DirectoryNode)
            Next
            For Each File As IO.FileInfo In NodeDirectory.GetFiles
                Dim FileNode As New TreeNode
                FileNode.Tag = File.FullName
                FileNode.Text = File.Name
                e.Node.Nodes.Add(FileNode)
            Next
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
        End Try
    End Sub


:rolleyes:/>/>

But your works are always extraordinary, I will take it as an example of the study. :smile2:/>/>
Thank Razer...you're number one in this forum... :yes:/>/>

This post has been edited by Dreamfall: 09 January 2017 - 04:57 AM

Was This Post Helpful? 1
  • +
  • -

#12 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1413
  • View blog
  • Posts: 3,614
  • Joined: 01-February 13

Re: Add logical drives in a browse treeview...

Posted 09 January 2017 - 09:20 AM

Quote

Thank Razer...you're number one in this forum...

Well, i wouldn`t go that far but, i`m glad you got it figured out. Also, thanks for sharing your solution for others to learn from. 8)
Was This Post Helpful? 0
  • +
  • -

#13 maceysoftware  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 286
  • View blog
  • Posts: 1,311
  • Joined: 07-September 13

Re: Add logical drives in a browse treeview...

Posted 09 January 2017 - 09:56 AM

A little off topic, however dreamfall your latest example for creating the desktop and documents treenode can end up being alot of repeated code.

Just to show you how you can cut down on this I have just used your code there and jumbled it up ever so slightly so that the code is re-usable.

Feel free to ignore this however I find that when I start writing repeating code its time to take a break, grab and drink and think if there is a way I can make it generic. Sometimes you can't.

Public Class Form1

    Dim Desktop As New TreeNode()
    Dim Music As New TreeNode()
    Dim Documents As New TreeNode()
    Dim Pictures As New TreeNode()

' I am using the sub new just because i didn't know where the following code was being called from.

'Me.Load ....
'       Me.Create_Documents(BrowseTreeViewOne)
'       Me.Create_Desktop(BrowseTreeViewOne)
'End Sub

' However think of this Sub New to be where you are calling this code.
    Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Desktop = Create_SpecialDirectoryTreeNode("My Desktop", FileIO.SpecialDirectories.Desktop)
        Music = Create_SpecialDirectoryTreeNode("My Music", FileIO.SpecialDirectories.MyMusic)
        Documents = Create_SpecialDirectoryTreeNode("My Documents", FileIO.SpecialDirectories.MyDocuments)
        Pictures = Create_SpecialDirectoryTreeNode("My Pictures", FileIO.SpecialDirectories.MyPictures)

    End Sub

    ' Ok well i changed the function to return a treenode seeing that your return value was always returning false.
    Public Function Create_SpecialDirectoryTreeNode(description As String, path As String) As TreeNode
        ' Dim Return_Value As Boolean = False
        Try
            ' You could pass the description when initialing the treenode however
            ' as your code didn't have that i will leave that commented out.
            ' Dim specialNode As New TreeNode(description)
            Dim specialNode As New TreeNode
            specialNode.Text = description
            specialNode.Tag = path
            specialNode.Nodes.Add("")
            ' So here i am still adding to the treeview but not the one that 
            ' is being passed in just the one on the form, depending on where 
            ' the treeview and this code is located you may want to still pass 
            ' in the treeview and use that.
            TreeView1.Nodes.Add(specialNode)
            Return specialNode
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            '  Return_Value = False
            Return Nothing
        End Try
        '  Return Return_Value
    End Function


End Class

This post has been edited by maceysoftware: 09 January 2017 - 09:57 AM

Was This Post Helpful? 1
  • +
  • -

#14 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1413
  • View blog
  • Posts: 3,614
  • Joined: 01-February 13

Re: Add logical drives in a browse treeview...

Posted 09 January 2017 - 12:33 PM

That is similar to what i did in my tutorial i gave a link to last night. Instead of repeating similar code several times to add each of the different special folders, the code is only added to the program once in the AddSpecialFolderRootNode sub. Then, all that is needed is to call the sub one or more times to add the folder(s) that you want added as root nodes. 8)
Was This Post Helpful? 0
  • +
  • -

#15 Dreamfall  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 107
  • Joined: 26-January 15

Re: Add logical drives in a browse treeview...

Posted 09 January 2017 - 01:01 PM

@maceysoftware ...Excellent code squeezed :smile2:/>/>/>/>

Guys one last thing, how do I change the image of the icon only to node (0) with an image file resource...?
I wanted to add a title to the first parent node in this way:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim Drives As Collections.ObjectModel.ReadOnlyCollection(Of IO.DriveInfo) =       My.Computer.FileSystem.Drives
        Me.BrowseTreeViewOne.Nodes.Clear()

        Me.BrowseTreeViewOne.Nodes.Add("TITLE") ' Is NODE (0) Change Icon Only Node 0
        Load_Images() ' Assign the ImageList to the TreeView.

        For I As Integer = 0 To Drives.Count - 1
            If Not (Drives(I).IsReady) Then
                Continue For
            End If
            RootNode = New TreeNode(Drives(I).Name.ToString)
            RootNode.Tag = Drives(I).Name.ToString
            RootNode.Nodes.Add("*DUMMY*")
            Me.BrowseTreeViewOne.Nodes.Add(RootNode)
            RootNode.ImageKey = CacheShellIcon(Drives(I).Name)
            RootNode.SelectedImageKey = RootNode.ImageKey
        Next
    End Sub



I tried this but change the image of all of the nodes folders and subfolders...

Private Sub Load_Images()
        Dim myImageList As New ImageList
        myImageList.Images.Add(My.Resources.IMG)

        Me.BrowseTreeViewOne.ImageList = myImageList

        Me.BrowseTreeViewOne.Nodes(0).ImageIndex = 0

    End Sub


I need to change only the image of the first node (0) of the treeview, everything else it must be controlled by the "SHGetFileInfo" function.. :smile2:/>
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2