Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
2.60/5 (2 votes)
See more:
Ok so this is all most working but now I need to see it work and need just a bit of further assistance to get the desired effect that im looking for. the code I have so far for my treeview is:
VB
Imports System.IO
Public Class form1

    Private Sub form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        tv1.Nodes.Clear()
        For Each drv As DriveInfo In My.Computer.FileSystem.Drives
            If drv.IsReady Then Call LoadFolderTree(drv.Name)
        Next
        tv1.Nodes(0).Expand()
        On Error Resume Next
        tv1.Nodes(1).Expand()
    End Sub

    Public Sub LoadFolderTree(ByVal path As String)
        Dim basenode As System.Windows.Forms.TreeNode, volbl As String, drvltr As String = path
        If IO.Directory.Exists(path) Then
            If path.Length <= 3 Then
       volbl = My.Computer.FileSystem.GetDriveInfo(path).VolumeLabel
       If Len(drvltr) = 3 Then drvltr = Mid(drvltr, 1, 2)
       basenode = tv1.Nodes.Add(String.Format("{0} ({1})", volbl, drvltr))
            Else
            basenode = tv1.Nodes.Add(My.Computer.FileSystem.GetName(path))
            End If
            basenode.Tag = path
            LoadDir(path, basenode)
        Else
            Throw New DirectoryNotFoundException()
        End If
    End Sub

    Private Sub tv1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tv1.AfterSelect
        lblMessage.Text = tv1.SelectedNode.Tag
    End Sub

    Private Sub tv1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles tv1.KeyDown
        If e.KeyCode = Keys.Enter Then
            Dim snd As TreeNode = tv1.SelectedNode
            On Error Resume Next
            tv1.CollapseAll()
            snd.Expand()
            tv1.SelectedNode = snd
        End If
    End Sub

Private Sub tv1_AfterExpand(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tv1.AfterExpand
        Dim n As System.Windows.Forms.TreeNode
        For Each n In e.Node.Nodes
            LoadDir(n.Tag, n)
        Next
    End Sub
Public Sub LoadDir(ByVal DirPath As String, ByVal Node As Windows.Forms.TreeNode)
        On Error Resume Next
        Dim Dir As String, DirName As String, Index As Integer
        If Node.Nodes.Count = 0 Then
        For Each Dir In My.Computer.FileSystem.GetDirectories(DirPath)
        Index = Dir.LastIndexOf("\") : DirName = Dir.Substring(Index + 1, Dir.Length - Index - 1)
       If fncAddThisDir(DirName) Then Node.Nodes.Add(DirName) : Node.LastNode.Tag = Dir : Node.LastNode.ImageIndex = 0
            Next
        End If
    End Sub

    Function fncAddThisDir(ByVal dirname As String) As Boolean
        If Mid(LCase(dirname), 1, 7) = "program" Then dirname = "Program"
        Select Case LCase(dirname)
            Case ""
            Case "$avg","$recycle.bin"
            Case "recycler", ".wd_tv"
            Case Else : fncAddThisDir = True
        End Select
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        LookAtDir(lblMessage.Text.ToString)

    End Sub
    Private Sub LookAtDir(ByVal path As String)
        Try
            For Each dir As String In Directory.GetDirectories(path)
                LookAtDir(dir)
            Next
            Do something with the directory.
        Catch ex As UnauthorizedAccessException
        End Try
    End Sub
End Class

In short I want a treeview that can show all hard drives and their folders/subfolders. The user then clicks the desired root such as C:\ or D:\ ect and clicks start........
The textbox then shows the current folder being scanned untill eof....
Posted
Updated 2-Feb-11 14:44pm
v6
Comments
Kschuler 2-Feb-11 14:40pm    
So what part isn't working?
Dale 2012 2-Feb-11 19:17pm    
I need to be able to see the current folder being scanned in the lblmessage textbox and also Id like to be able to see a msgbox appear when the scan has finished. If there is any mistakes or improvments that can be made id also like some suggestions about that if possible.

thanks in advance.. :)

Hi,

1. Current folder name in lblmessage textbox. I can not see anywhere that you are assigning current folder's name to the text property of the textbox hence you will be able to see only the name of the folder which u have selected in the treeview, not the children folder names.
For that you need to modify LookAtDir method where you assign path to the lblMessage.Text Property. you should write the following statement in LookAtDir method before foreach statement.

lblMessage.Text= path;


2. Messagebox when scan has finished. In Button_Click, after calling LookAtDir Method you should write code to display messagebox.

I hope this will help you.

Thanks and regards,
Chetan Ranpariya
 
Share this answer
 
Comments
Dale 2012 3-Feb-11 2:08am    
I appricate your response but need the lblmessage.text to display the files folders and subfolders that are currently being scanned. Like all scanners the name of the directory being scanned is displayed. How can I do this in my case?
Hi,

1. Current folder name in lblmessage textbox. I can not see anywhere that you are assigning current folder's name to the text property of the textbox hence you will be able to see only the name of the folder which u have selected in the treeview, not the children folder names.
For that you need to modify LookAtDir method where you assign path to the lblMessage.Text Property. you should write the following statement in LookAtDir method before foreach statement.

lblMessage.Text= path;


2. Messagebox when scan has finished. In Button_Click, after calling LookAtDir Method you should write code to display messagebox.

I hope this will help you.

Thanks and regards,
Chetan Ranpariya
 
Share this answer
 
Comments
Dale 2012 3-Feb-11 2:07am    
I appricate your response but need the lblmessage.text to display the files folders and subfolders that are currently being scanned. Like all scanners the name of the directory being scanned is displayed. How can I do this in my case?
add this in loops

application.doevent();

it gives you the look and feel.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900