I posted this on another Forum but haven't been lucky to get any replies and I just found this forum (new to VB.Net) I came across these two code snippets and would like to find a way to combine them. Essentially what I am looking to do is populate a list box with a list of everything in a directory (sub-directories, files) and then use a Textbox to filter out the list.
The top code snippet does a good job at filtering but doesn't dig deep enough down through sub-directories as the second code snippet. Please show me how to combine the two.
Thank you in advance.
The top code snippet does a good job at filtering but doesn't dig deep enough down through sub-directories as the second code snippet. Please show me how to combine the two.
Thank you in advance.
VB.NET:
[COLOR=#00008B][FONT=Consolas]Public[/FONT][/COLOR][COLOR=#000000][FONT=Consolas] [/FONT][/COLOR][COLOR=#00008B][FONT=Consolas]Class[/FONT][/COLOR][COLOR=#000000][FONT=Consolas] Form1[/FONT][/COLOR] [COLOR=#00008B]Dim[/COLOR] bs [COLOR=#00008B]As[/COLOR] [COLOR=#00008B]New[/COLOR] BindingSource
[COLOR=#00008B]Public[/COLOR] [COLOR=#00008B]Sub[/COLOR] [COLOR=#00008B]New[/COLOR]()
InitializeComponent()
[COLOR=#00008B]End[/COLOR] [COLOR=#00008B]Sub[/COLOR]
[COLOR=#00008B]Protected[/COLOR] [COLOR=#00008B]Overrides[/COLOR] [COLOR=#00008B]Sub[/COLOR] OnLoad([COLOR=#00008B]ByVal[/COLOR] e [COLOR=#00008B]As[/COLOR] System.EventArgs)
[COLOR=#00008B]MyBase[/COLOR].OnLoad(e)
[COLOR=#00008B]Dim[/COLOR] testPath [COLOR=#00008B]As[/COLOR] [COLOR=#00008B]String[/COLOR] = [COLOR=#800000]"c:\MyPath"[/COLOR]
[COLOR=#00008B]Dim[/COLOR] dt [COLOR=#00008B]As[/COLOR] [COLOR=#00008B]New[/COLOR] DataTable
dt.Columns.Add([COLOR=#800000]"File"[/COLOR], [COLOR=#00008B]GetType[/COLOR]([COLOR=#00008B]String[/COLOR]))
[COLOR=#00008B]For[/COLOR] [COLOR=#00008B]Each[/COLOR] f [COLOR=#00008B]As[/COLOR] [COLOR=#00008B]String[/COLOR] [COLOR=#00008B]In[/COLOR] Directory.GetFiles(testPath)
[COLOR=#00008B]Dim[/COLOR] row [COLOR=#00008B]As[/COLOR] DataRow = dt.NewRow
row([COLOR=#800000]"File"[/COLOR]) = Path.GetFileName(f)
dt.Rows.Add(row)
[COLOR=#00008B]Next[/COLOR]
bs.DataSource = dt
ListBox1.DisplayMember = [COLOR=#800000]"File"[/COLOR]
ListBox1.ValueMember = [COLOR=#800000]"File"[/COLOR]
ListBox1.DataSource = bs
[COLOR=#00008B]End[/COLOR] [COLOR=#00008B]Sub[/COLOR]
[COLOR=#00008B]Private[/COLOR] [COLOR=#00008B]Sub[/COLOR] TextBox1_TextChanged([COLOR=#00008B]ByVal[/COLOR] sender [COLOR=#00008B]As[/COLOR] [COLOR=#00008B]Object[/COLOR], [COLOR=#00008B]ByVal[/COLOR] e [COLOR=#00008B]As[/COLOR] EventArgs) [COLOR=#00008B]Handles[/COLOR] TextBox1.TextChanged
bs.Filter = [COLOR=#00008B]String[/COLOR].Format([COLOR=#800000]"File LIKE '*{0}*'"[/COLOR], TextBox1.Text)
[COLOR=#00008B]End[/COLOR] [COLOR=#00008B]Sub[/COLOR]
[COLOR=#00008B][FONT=Consolas]End[/FONT][/COLOR][COLOR=#000000][FONT=Consolas] [/FONT][/COLOR][COLOR=#00008B][FONT=Consolas]Class[/FONT][/COLOR]
VB.NET:
Dim stack As New System.Collections.Generic.Stack(Of System.IO.DirectoryInfo)
'add the start folder
stack.Push(New System.IO.DirectoryInfo("C:\folder1\"))
While stack.Count > 0
Dim di As System.IO.DirectoryInfo = stack.Pop
'process files in current directory as you need.
For Each f As System.IO.FileInfo In di.GetFiles
'here, we just add the filename to a listbox
Me.ListBox1.Items.Add(f.FullName)
Next
Try
'add subdirectories to the stack to be processed
For Each subDi As System.IO.DirectoryInfo In di.GetDirectories
stack.Push(subDi)
Next
Catch ex As System.UnauthorizedAccessException
'beware access denied errors
'handle as appropriate
End Try
End While