[RESOLVED] [2005]Searching files and folders
Hello everyone..
I have this problem in making a simple search like the windows search to search files and folders.
I want it to search within the specified folder and subfolders inside it...and return all the files matching the query.
Here is what Ive tried.,.
VB Code:
Dim s As String = TextBox1.Text
For Each file As String In My.Computer.FileSystem.GetFiles("c:\", FileIO.SearchOption.SearchAllSubDirectories, s)
ListView1.Items.Add(file)
Next file
.but the problem is...My program freezes till the search ends unlike the windows search.
Someone help...Thank you so much in advance :-)
Re: Searching files and folders
you would have to do the search in another thread so your UI can be free to refresh. There are different ways to do it. In 2005, there is a Background Worker, although I have never used it. There is also the System.Threading.Thread class, as well as the option to use Asunchronous delegates...
Check out this thread:
http://www.vbforums.com/showthread.php?t=399009
Searching this forum for "thread", "multithreading", "backgroundworker", or things like that should pull some examples...
There is also a codebank submission on recursive filesearching that may have different ways to do it...
Re: [2005]Searching files and folders
Hi man,
I tried this now....
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ts As New Threading.Thread(AddressOf searchfiles)
ts.Start()
End Sub
Private Sub searchfiles()
Dim s As String = TextBox1.Text
For Each file As String In My.Computer.FileSystem.GetFiles("c:\", FileIO.SearchOption.SearchAllSubDirectories, s)
ListView1.Items.Add(file)
Next file
End Sub
But,well it doesn't freeze my application...but doesnt update the listview as it searches either...Is it supposed to be a more complicated job to achieve this?
Thanks :)
Re: [2005]Searching files and folders
Well, I tested this code on mine (in 2003), and it seemed to work fine...
VB Code:
'in a sub
Dim MyThread As New System.Threading.Thread(AddressOf DoSomething)
MyThread.Start()
Private Sub DoSomething()
ListBox1.Items.Add("this")
System.Threading.Thread.Sleep(5000)
ListBox1.Items.Add("that")
End Sub
It added "this".. waited 5 seconds, then added "that"... so Im not too sure why its not working for you... unless that method isnt returning any results...
Re: [2005]Searching files and folders
You shouldn't be adding items to a ListView directly from a thread other than the UI thread. Also, there is no "as it searches". GetFiles is a synchronous method. It starts and doesn't return until it has got all the matching files, which it returns in a ReadOnlyCollection. Your code will get to the start of the For Each loop and GetFiles will block until all files are retrieved, then the loop will start iterating through the items in the collection it returns. If you want it to update along the way then you're going to have to create your own recursive algorithm that adds the files along the way, and then also make sure that you're using a delegate to marshal your control access to the UI thread.
Re: [2005]Searching files and folders
Hi,
On VS2005...even the code yoiu have shown me doesnt work..
This is what it says...
"Cross-thread operation not valid: Control 'ListBox1' accessed from a thread other than the thread it was created on."
Any idea what can be done?
Re: [2005]Searching files and folders
Hi Jm,
you mean then its got a lot of work to be done?
Re: [2005]Searching files and folders
Check out this sample that does exactly what you are trying to do all wrapped up into a control...
http://msdn.microsoft.com/library/de...rmscontrol.asp
Re: [2005]Searching files and folders
A lot or a little, depending on what you want. Marshaling the control member access to the UI thread is no big deal, although if you haven't done it before then you might not understand what's happening. Basically, you cannot safely access members of a control (other than Invoke, BeginInvoke, EndInvoke and CreateGraphics) on any thread other than the main UI thread. There is a way to simply ignore this and do it anyway but I wouldn't recommend it unless you know for certain that it will work. What you need to do is create a delegate for the method you want to call and it will cross the thread boundary and execute the method on the appropriate thread. Here's what your code would look like with that change:
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ts As New Threading.Thread(AddressOf searchfiles)
ts.Start()
End Sub
Private Sub searchfiles()
Dim s As String = TextBox1.Text
Dim files As ObjectModel.ReadOnlyCollection(Of String) = My.Computer.FileSystem.GetFiles("c:\", FileIO.SearchOption.SearchAllSubDirectories, s)
Me.AddFiles(files)
End Sub
Private Delegate Sub AddFilesDelegate(ByVal files As ObjectModel.ReadOnlyCollection(Of String))
Private Sub AddFiles(ByVal files As ObjectModel.ReadOnlyCollection(Of String))
If Me.ListView1.InvokeRequired Then
'This is not the UI thread so use a delegate to marshal the call to the UI thread.
Me.ListView1.Invoke(New AddFilesDelegate(AddressOf AddFiles), files)
Else
'This is the UI thread so add the items.
For Each file As String In files
Me.ListView1.BeginUpdate()
Me.ListView1.Items.Add(file)
Me.ListView1.EndUpdate()
Next file
End If
End Sub
That will get all the files first and then add them all in one go. Liike I said, if you actually want them added as they are found then that will require more work.
Re: [2005]Searching files and folders
nice example JM :thumb: (adding it to my threading bookmarks :p)
Re: [2005]Searching files and folders
Hi Jm and Gigem,
Im not able to search by the name of the file...It only works if I search like "*.txt"...I wonder why? :-?
Re: [2005]Searching files and folders
Show us the code that doesn't work.
Re: [2005]Searching files and folders
Oops,sorry Jm,It works perfectly...
I was so dumb to type part of the filename and search...Thats why it didnt work...:)
Thanks a tonne...
I'm trying out both your codes only :-)