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.EventArgs) Handles 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.
Update Contact :
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email : Fajarudinsidik@gmail.com
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email: Fajarudinsidik@gmail.com
atau Kirimkan Private messanger melalui email dengan klik tombol order dibawah ini :