Searching for files using vb.net 2005
-
I stole this snippet from a C# forum and converted it into VB. It's a routine for searching files recursively. When i test it however, I get the "Access to the path 'C:\System Volume Information' is denied." error. I've tried the old VB6: On Error Resume Next. Doesn't work thou. Any way to get around it? Private Sub DirSearch(ByVal sDir As String) Dim arrLst As New ArrayList Dim _dir As String Dim _file As String Try For Each _dir In Directory.GetDirectories(sDir) For Each _file In Directory.GetFiles(_dir, txtFile.Text) arrLst.Add(_file) Next DirSearch(_dir) Next Catch ex As Exception Debug.Print(ex.Message) End Try End Sub Thanks
-
I stole this snippet from a C# forum and converted it into VB. It's a routine for searching files recursively. When i test it however, I get the "Access to the path 'C:\System Volume Information' is denied." error. I've tried the old VB6: On Error Resume Next. Doesn't work thou. Any way to get around it? Private Sub DirSearch(ByVal sDir As String) Dim arrLst As New ArrayList Dim _dir As String Dim _file As String Try For Each _dir In Directory.GetDirectories(sDir) For Each _file In Directory.GetFiles(_dir, txtFile.Text) arrLst.Add(_file) Next DirSearch(_dir) Next Catch ex As Exception Debug.Print(ex.Message) End Try End Sub Thanks
Add some try/catch block to your code and handle the exception. -------------------------------------------------------- My portfolio & development blog Q:What does the derived class in C# tell to it's parent? A:All your base are belong to us!
-
I stole this snippet from a C# forum and converted it into VB. It's a routine for searching files recursively. When i test it however, I get the "Access to the path 'C:\System Volume Information' is denied." error. I've tried the old VB6: On Error Resume Next. Doesn't work thou. Any way to get around it? Private Sub DirSearch(ByVal sDir As String) Dim arrLst As New ArrayList Dim _dir As String Dim _file As String Try For Each _dir In Directory.GetDirectories(sDir) For Each _file In Directory.GetFiles(_dir, txtFile.Text) arrLst.Add(_file) Next DirSearch(_dir) Next Catch ex As Exception Debug.Print(ex.Message) End Try End Sub Thanks
Well, that's because you don't have enough permission to access that directory. You can just ignore that by moving the Try Block inside the loop, like this :
Private Sub DirSearch(ByVal sDir As String)
Dim arrLst As New ArrayList
Dim _dir As String
Dim _file As StringFor Each _dir In Directory.GetDirectories(sDir)
Try
For Each _file In Directory.GetFiles(_dir, txtFile.Text)
Try
arrLst.Add(_file)
Catch ex as IOException
Continue
End Catch
Next
DirSearch(_dir)
Catch ex as IOException
Continue
End Try
NextEnd Sub
That should fix it. The Continue statements continue the loop into the next iteration, so that exception should be ignored.. Yuvi Panda T 15 year old Microsoft Student Partner Blogs at : http://yuvipanda.blogspot.com