Fill a TreeView from a Basic recursive search file
Basic recursive search file
I pick a code from one of my previous post : search file search file - Improve performance . In that post a made a recursive function to search file file and directory over and over. All the result are injected in a ListView and not in a TreeView. Enventually, we will have to adapt the code because a ListView and a TreeView works differently.
While ListView group the items in a collection, Treeview are grouping the data with nodes.
Please take a look at the code and go to the next post.
Private Sub findfiles() Dim dirPath As String Dim path_valid As Boolean path_valid = True dirPath = "c:\temp\" ' temp = no access problem dirPath = "c:\System Recovery" ' System Recovery = access problem '---------------------------------------------- ' CHECK PATH IF VALID '---------------------------------------------- 'trim the sPath to remove space before and after the String dirPath = Trim(dirPath) 'check if sPath is not zero length If Len(dirPath) > 0 Then Else path_valid = False End If 'check for invalid char ' \ * | : ? < > / except the \ because is a directory seperator ' and * that means to ignore whatever inside the other chars. (optional) If InStr(dirPath, "|", CompareMethod.Binary) > 0 Then path_valid = False End If If InStr(dirPath, ">", CompareMethod.Binary) > 0 Then path_valid = False End If If InStr(dirPath, "<", CompareMethod.Binary) > 0 Then path_valid = False End If If InStr(dirPath, "?", CompareMethod.Binary) > 0 Then path_valid = False End If If InStr(dirPath, ":", CompareMethod.Binary) = 2 Then 'very basic way to check if there is a ":" in the Path. Dim str_temp As String = dirPath.Substring(2, Len(dirPath) - 2) If InStr(str_temp, ":", CompareMethod.Binary) > 0 Then path_valid = False End If End If If InStr(dirPath, "*", CompareMethod.Binary) > 0 Then path_valid = False End If 'be careful with non-english caracters '---------------------------------------------- ' SECURITY '---------------------------------------------- Dim oDirectorySecurity As System.Security.AccessControl.DirectorySecurity Dim oAuthorizationRuleCollection As System.Security.AccessControl.AuthorizationRuleCollection Dim oRule As System.Security.AccessControl.FileSystemAccessRule Dim Security_valid As Boolean Security_valid = False oDirectorySecurity = IO.Directory.GetAccessControl(dirPath) oAuthorizationRuleCollection = oDirectorySecurity.GetAccessRules(True, True, Type.GetType("System.Security.Principal.NTAccount")) For Each oRule In oAuthorizationRuleCollection If oRule.FileSystemRights = Security.AccessControl.FileSystemRights.FullControl Then If InStr(oRule.IdentityReference.Value, My.User.Name) > 0 Then Security_valid = True End If End If Next '---------------------------------------------- ' SECURITY '---------------------------------------------- Dim Is_Folder_shortcut As Boolean Is_Folder_shortcut = False If dirPath.StartsWith("c:\Users", True, System.Globalization.CultureInfo.CurrentCulture) Then 'very basic way to force you to be very carefull with that folder Is_Folder_shortcut = True End If '---------------------------------------------- ' IF ALL OK, THEN DISPLAY THE RESULT '---------------------------------------------- If oForm2 Is Nothing OrElse oForm2.IsDisposed Then oForm2 = New Form2 oForm2.Show() End If oForm2.ListBox1.Items.Clear() If path_valid = True And Security_valid = True And Is_Folder_shortcut = False Then For Each foundFile As String In My.Computer.FileSystem.GetFiles( _ dirPath, FileIO.SearchOption.SearchAllSubDirectories, "*.dll") oForm2.ListBox1.Items.Add(foundFile) Next Else MsgBox("path_valid =" & path_valid & vbCrLf & "Security_valid =" & Security_valid) End If End Sub |
Now, this is a new code with minor change. All modifications are highlighted in yellow.
Private Sub findfiles(Optional dirPath As String = "c:\temp\", Optional String_to_Search As String = "*.dll") 'Dim dirPath As String ' you need to put in argument a "top" directory (optional if you want) Dim path_valid As Boolean path_valid = True 'dirPath = "c:\temp\" ' temp = no access problem 'dirPath = "c:\System Recovery" ' System Recovery = access problem '---------------------------------------------- ' CHECK PATH IF VALID '---------------------------------------------- 'trim the sPath to remove space before and after the String dirPath = Trim(dirPath) 'check if sPath is not zero length If Len(dirPath) > 0 Then Else path_valid = False End If 'check for invalid char ' \ * | : ? < > / except the \ because is a directory seperator ' and * that means to ignore whatever inside the other chars. (optional) If InStr(dirPath, "|", CompareMethod.Binary) > 0 Then path_valid = False End If If InStr(dirPath, ">", CompareMethod.Binary) > 0 Then path_valid = False End If If InStr(dirPath, "<", CompareMethod.Binary) > 0 Then path_valid = False End If If InStr(dirPath, "?", CompareMethod.Binary) > 0 Then path_valid = False End If If InStr(dirPath, ":", CompareMethod.Binary) = 2 Then 'very basic way to check if there is a ":" in the Path. Dim str_temp As String = dirPath.Substring(2, Len(dirPath) - 2) If InStr(str_temp, ":", CompareMethod.Binary) > 0 Then path_valid = False End If End If If InStr(dirPath, "*", CompareMethod.Binary) > 0 Then path_valid = False End If 'be careful with non-english caracters '---------------------------------------------- ' SECURITY '---------------------------------------------- Dim oDirectorySecurity As System.Security.AccessControl.DirectorySecurity Dim oAuthorizationRuleCollection As System.Security.AccessControl.AuthorizationRuleCollection Dim oRule As System.Security.AccessControl.FileSystemAccessRule Dim Security_valid As Boolean Security_valid = False oDirectorySecurity = IO.Directory.GetAccessControl(dirPath) oAuthorizationRuleCollection = oDirectorySecurity.GetAccessRules(True, True, Type.GetType("System.Security.Principal.NTAccount")) For Each oRule In oAuthorizationRuleCollection If oRule.FileSystemRights = Security.AccessControl.FileSystemRights.FullControl Then If InStr(oRule.IdentityReference.Value, My.User.Name) > 0 Then Security_valid = True End If End If Next '---------------------------------------------- ' SECURITY '---------------------------------------------- Dim Is_Folder_shortcut As Boolean Is_Folder_shortcut = False If dirPath.StartsWith("c:\Users", True, System.Globalization.CultureInfo.CurrentCulture) Then 'very basic way to force you to be very carefull with that folder Is_Folder_shortcut = True End If '---------------------------------------------- ' IF ALL OK, THEN DISPLAY THE RESULT '---------------------------------------------- If oForm2 Is Nothing OrElse oForm2.IsDisposed Then oForm2 = New Form2 oForm2.Show() End If 'oForm2.ListBox1.Items.Clear() ' recursive search file : don't clear the listBox If path_valid = True And Security_valid = True And Is_Folder_shortcut = False Then 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 extention in search argument 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 Else MsgBox("path_valid =" & path_valid & vbCrLf & "Security_valid =" & Security_valid) End If End Sub |
Previous Page : Fill a TreeView with recursivity <<< = = =
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 :