System.UnauthorizedAccessException

89tqYvEj7b

New member
Joined
Mar 4, 2010
Messages
3
Programming Experience
10+
I am writing a class to list the files in a folder or drive. I am using vb.net express 2010 installed on a Win7 64 bit machine. VB.net was installed using an account with admin rights to the machine and the network on which it resides. I have been stopped cold by a System.UnauthorizedAccessException. I have tried changing the manifest, as recommended elsewhere. The exception remains. I can read any file in Windows Explorer. I can use a VBA class using Win32 API calls. But I cannot seem to overcome the exception using managed code. Below is a code fragment that is producing the error. Any suggestions would be helpful.
   Private Sub sLoadCatalog(ByVal strPath As String, ByVal intParentID As Integer)
        'Get the file list
        Dim strsearchPattern As String = "*"
        Dim fp As New FileIOPermission(FileIOPermissionAccess.PathDiscovery Or FileIOPermissionAccess.Read, strPath)
        Try
            'fp.Demand()
            fp.Assert()
            Dim dirInfo As DirectoryInfo = New DirectoryInfo(strPath)
            Dim FileList() As FileInfo = dirInfo.GetFiles()

            If FileList.Length > 0 Then
                Dim intLow As Integer = FileList.GetLowerBound(0)
                Dim intHigh As Integer = FileList.GetUpperBound(0)
                For intCur As Integer = intLow To intHigh
                    Dim strfile As String = FileList(intCur).FullName
                    Dim clOneFile As New ClassFileItem
                    With clOneFile
                        intLastID += 1
                        .Item_ID = intLastID
                        .ItemName = FileList(intCur).Name
                        .ItemType = "File"
                        .lastAccessed = FileList(intCur).LastAccessTime
                        .Parent_ID = intParentID
                        .ParentFolderPath = FileList(intCur).DirectoryName
                        .SizeInBytes = FileList(intCur).Length
                        .FullPath = FileList(intCur).FullName
                        .DateCreated = FileList(intCur).CreationTime
                        .DateModified = FileList(intCur).LastWriteTime
                        .Extension = FileList(intCur).Extension
                    End With
                    Dim strKey As String = "N" & CStr(intLastID)
                    colFileList.Add(clOneFile, strKey)

                Next

                'Else
                'Exit Sub
            End If

            Dim dirFolder() As DirectoryInfo = dirInfo.GetDirectories
            If dirFolder.Length > 0 Then
                'recursive call
                Dim intLow As Integer = dirFolder.GetLowerBound(0)
                Dim intHigh As Integer = dirFolder.GetUpperBound(0)
                For intCur As Integer = intLow To intHigh
                    Dim strfolder As String = dirFolder(intCur).FullName
                    Dim clNewFolder As New ClassFileItem
                    With clNewFolder
                        intLastID += 1
                        .Item_ID = intLastID
                        .ItemName = dirFolder(intCur).Name
                        .DateCreated = dirFolder(intCur).CreationTime
                        .DateModified = dirFolder(intCur).LastWriteTime
                        .lastAccessed = dirFolder(intCur).LastAccessTime
                        .FullPath = dirFolder(intCur).FullName
                        .ItemType = "Folder"
                        .Parent_ID = intParentID
                        .ParentFolderPath = dirFolder(intCur).FullName
                    End With
                    Dim strKey As String = "N" & CStr(intLastID)
                    colFileList.Add(clNewFolder, strKey)
                    sLoadCatalog(strfolder, intLastID)
                Next
            Else
                Exit Sub
            End If
        Catch e As SecurityException
            'MessageBox.Show("Security Cannot access folder " & strPath)
        Catch k As UnauthorizedAccessException
            'MessageBox.Show("Unauthorized Cannot access folder " & strPath)
        End Try
    End Sub
 
Last edited by a moderator:
If you traverse the whole file system on your system drive then you will undoubtedly hit at least one inaccessible folder. It's a matter of where you put your exception handler. Here's an example of a recursive file search that ignores such folders and moves on.
Private Sub ProcessFiles(folderPath As String)
    Try
        For Each filePath As String In Directory.GetFiles(folderPath)
            'Use filePath here.
        Next

        For Each subfolderPath As String In directory.GetDirectories(folderPath)
            ProcessFiles(subfolderPath)
        Next
    Catch ex As UnauthorizedAccessException
        'Ignore inaccessible folder and continue.
    End Try
End Sub
The method tries to access the contents of one folder. If it succeeds then it calls itself once for each subfolder and if it fails it just returns immediately and that folder is ignored.
 
Some folders can't be read by user by any means (for example "System Volume Information"), while other folders may be restricted for some users or even restricted in the security mode the current users operate, this also applies to admin acounts. Setting requireAdministrator in manifest, or starting the process with 'run as admin', will take you to the limit of what that user account can do when run as elevated admin under UAC.
 
Back
Top