views:

93

answers:

2

Hey guys,

I have this small file search engine here made in VB.NET: ListBox1.Items.Clear() ListBox3.Items.Clear()

    ChDir("C:\")
    Try
        For Each foundFile As String In My.Computer.FileSystem.GetFiles( _
            My.Computer.FileSystem.CurrentDirectory, _
            FileIO.SearchOption.SearchAllSubDirectories, TextBox4.Text & "*.*")
            ListBox1.Items.Add(foundFile)
            ListBox3.Items.Add(foundFile)

        Next
    Catch ex As UnauthorizedAccessException
        MsgBox("Could not access file or not enough priveledges")
    End Try

It searches through your whole C:\ for the file you entered. Although the problem I get is that some directories get access denied or not existing directories. How can I fix this problem?

Thanks

A: 

By granting privileges to the denied directories, and closing the programs that are locking the files within the directories.

MSDN says that, within the context of the GetFiles Method, an UnauthorizedAccessException means that the user lacks necessary permissions. See http://msdn.microsoft.com/en-us/library/t71ykwhb(VS.80).aspx

I would imagine that some directories are reserved by the file system, and you are not allowed certain types of access regardless of your privileges.

Robert Harvey
+1  A: 

Some directories simply cannot be accessed like this. Use a try/catch loop with an empty catch to swallow errors and get the files that you can.

Try
    'code for testing goes here
Catch
End Try

The above code when implemented properly should work if no error is thrown, and if no error is thrown then nothing will happen.

Cyclone
Generally you shouldn't need to access these directories anyway, just so you know.
Cyclone
how would I do this?
Kevin
I put in some example code. Replace the comment with your own code of course.
Cyclone