Sunday, December 16, 2012

Implementing the changes to your TreeView

Implementing the changes



Ok, time to change the recursive function with the question you ask yourself. You may have to do some tries or test before getting something to work perfectly.  Normally, if you analyze your problem and ask yourself a lot of question, you should be able to make the changes easily.

Before :


Private Sub findfiles(Optional dirPath As String = "c:\temp\"Optional String_to_Search As String = "*.dll")



For Each foundFile As String In My.Computer.FileSystem.GetFiles( _
      dirPath, FileIO.SearchOption.SearchTopLevelOnly, String_to_Search)

       oForm2.ListBox1.Items.Add(foundFile)

Next

For Each foundDirectory As String In My.Computer.FileSystem.GetDirectories( _ dirPath, FileIO.SearchOption.SearchTopLevelOnly)

      findfiles(foundDirectory) ''recursive search file : re-use "himself"

Next




For Each foundFile As String In My.Computer.FileSystem.GetFiles( _
       dirPath, FileIO.SearchOption.SearchTopLevelOnly, String_to_Search) 'recursive search file : you need to limit your search to 1 single directory : and put extension in search argument

       'oForm2.ListBox1.Items.Add(foundFile)
       'TreeView1.Nodes.Add(foundFile)

       no1.Nodes.Add(foundFile)

Next

For Each foundDirectory As String In my.Computer.FileSystem.GetDirectories( dirPath, FileIO.SearchOption.SearchTopLevelOnly)

    'findfiles(foundDirectory) ''recursive search file : re-use "himself"
     Dim no2 As TreeNode
     no2 = no1.Nodes.Add(foundDirectory)
     findfiles(no2, foundDirectory, String_to_Search) ''recursive search file : re-use "himself"
Next



After:


Private Sub findfiles(no1 As TreeNode, Optional dirPath As String = "c:\temp\"Optional String_to_Search As String = "*.dll")


I added no1 for node one, this will represent the main node or current node. It works exactly the same way compared to dirPath. dirPath acts like the current path. All the stuff in the argument in a recursive function are constantly reused and is always base from the current data. The current data here in this example is the current node with a current directory.



For Each foundFile As String In My.Computer.FileSystem.GetFiles( _
       dirPath, FileIO.SearchOption.SearchTopLevelOnly, String_to_Search) 'recursive search file : you need to limit your search to 1 single directory : and put extension in search argument

       'oForm2.ListBox1.Items.Add(foundFile)
       'TreeView1.Nodes.Add(foundFile)

       no1.Nodes.Add(foundFile)

Next

For Each foundDirectory As String In my.Computer.FileSystem.GetDirectories( dirPath, FileIO.SearchOption.SearchTopLevelOnly)

    'findfiles(foundDirectory) ''recursive search file : re-use "himself"
     Dim no2 As TreeNode
     no2 = no1.Nodes.Add(foundDirectory)
     findfiles(no2, foundDirectory, String_to_Search) ''recursive search file : re-use "himself"
Next




And here how I decided to start my findfiles function. I injected a first node called MAIN.


    Private Sub Button1_Click(sender As System.Object, e As System.EventArgsHandles Button1.Click
        'position()
        'Fill_TreeView()
        Me.TreeView1.Nodes.Add("MAIN")
        Dim no As TreeNode
        no = Me.TreeView1.Nodes.Item(0)
        findfiles(no, "C:\ProgramData\Microsoft\Windows\Start Menu""*.lnk")
    End Sub



Previous Page : Analyse your code in Visual Basic <<<= = =








About 

I invite you to visit my blog for more articles and leave a comment. Check Technologies represents more than 10 years .... Computer and computer aided design.


235 Adrien-Provencher
Beloeil, (Québec), J3G 0C8, Canada
Tel : 514-705-7690
emal: info@checktechno.ca


No comments:

Post a Comment