I am just writing a simple file browser. It works without problems when i point to a specific directory but throws "UnauthorizedAccessException access to the path .... is denied" when i point to drive "C:\\" or "D:\\" appreciate any help.

If Not txtToSearch.Text = Nothing Then
            ListBox1.Items.Clear()
            FC = 0
            For Each foundfile As String In My.Computer.FileSystem.GetFiles("D:\\", FileIO.SearchOption.SearchAllSubDirectories, Nothing)
                Dim title As String = My.Computer.FileSystem.GetFileInfo(foundfile).Name
                If title.Contains(txtToSearch.Text) Then
                    ListBox1.Items.Add(title)
                    FC += 1
                End If
            Next

Dani AI

Generated

Short answer: the root enumeration hits system or protected folders (junctions like "C:\Documents and Settings" on newer Windows versions) and a single call that walks every subfolder will throw UnauthorizedAccessException as soon as it reaches one of them. was right to point out the offending folder; 's approach needs safe, per-directory error handling and a streaming enumerator so the UI and memory aren't overwhelmed.

Use a safe traversal that enumerates one directory at a time, catches and logs UnauthorizedAccessException (and IO exceptions) per directory, and skips reparse points (junctions/symlinks). The example below uses lazy enumeration so large drives do not get materialized in memory and allows the caller to update the ListBox incrementally or cancel the search from a background thread.

Imports System.IO

Iterator Function EnumerateFilesSafely(root As String, pattern As String) As IEnumerable(Of String)
    Dim stack As New Stack(Of String)
    stack.Push(root)
    While stack.Count > 0
        Dim dir = stack.Pop()
        Try
            For Each f In Directory.EnumerateFiles(dir, pattern)
                Yield f
            Next
        Catch ex As UnauthorizedAccessException
            Continue While
        Catch ex As IOException
            Continue While
        End Try

        Try
            For Each d In Directory.EnumerateDirectories(dir)
                Dim di = New DirectoryInfo(d)
                If (di.Attributes And FileAttributes.ReparsePoint) = 0 Then
                    stack.Push(d)
                End If
            Next
        Catch ex As UnauthorizedAccessException
            Continue While
        Catch ex As IOException
            Continue While
        End Try
    End While
End Function

Notes and troubleshooting tips: run file enumeration on a background thread (Task/BackgroundWorker) to keep the UI responsive; allow cancellation; limit depth or search patterns to reduce results; do not run the app as administrator just to avoid exceptions unless absolutely necessary. See Microsoft docs on handling these exceptions and on safe enumeration: UnauthorizedAccessException, Directory.EnumerateFiles and FileAttributes (ReparsePoint). For why GetFiles can fail when recursing an entire drive, see FileSystem.GetFiles.

Recommended Answers

All 3 Replies

Just curious, do you use "D:\\" or D:\" to access to the root directory of drive?

yes i tried both of them and got the exception in that case.

Ok.
The error generated by GetFiles, comes from a folder that you do not have enough privileges to read all the contents.

In the exception, you have the offending folder (ie: on Vista, "Access to the path 'C:\Documents and Settings' is denied".)

As this can happen in many others folders, I would suggest not to get all the file names in the drive with only one sentence.

Moreover, if you count the files and folder in the drive, you can get a lot of thousands, so this can be unmanageable in a listbox.

Hope this helps

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.