Question "Directory not Accessible" Property

Olhado_

New member
Joined
Jan 11, 2009
Messages
4
Location
Titusville, FL
Programming Experience
Beginner
I am in the process of creating a directory tree, using the treeview control. I realized there is a file directory control; but I did not want a separate window to pop up, in order to access it.

Anyways, I pretty much have working, except in a few cases, where it tries to access a folder that is inaccessible, even to me from Windows Explorer. When I try to access it, with Windows Explorer, I get "[Address] not accessible"; but when the .NET program tries to access it the same exception is thrown and the program does not recover (unlike Windows Explorer).

I do not care about accessing those directories (this is for a corporate program); but I would like to learn how to recover from that error by learning what property holds that exception.

I found the various access controls in System.Security namespace and tried using the canonicalAccess check, along with other permissions checks in that class; but it blocks folders I would like to access; but are listed as read-only, like "Document and Settings".

Does anyone have any advice of what other security properties I can access? It could be in VB or C# since I am pretty adapt at translating between them, even without a converter.

Thanks in advance.
 
You could use a Try/Catch while looping thru directories where you add them, it should just skip these. Also set a breakpoint and while debugging you will see which line it is this error falls on.
 
Thanks for the reply, and I am using a try/catch in the code, which is how I am getting the "Access Denied" message; but I am not getting any "code" with it. If I do not have a try/catch, then the program will just freeze with no message whatsoever.

I might try to post the code tomorrow, when I get back to the computer that has it.
 
E.g.
VB.NET:
Private Sub GetFiles(ByVal folder As String)
    Try
        For Each file In IO.Directory.GetFiles(folder)
            'Do something.
        Next

        For Each subfolder In IO.Directory.GetDirectories(folder)
            GetFiles(subfolder)
        Next
    Catch
        'Inaccessible.  Ignore and move on.
    End Try
End Sub
 
But that is the issue I am having what flag do I use to catch that one exception. I am having a hard time finding reference to that security setting and how to program around it.


VB.NET:
    Sub BuildDirectoryTree(ByVal fromnode As TreeNode, ByVal Dir As String)
        Dim newDirectory As TreeNode
        Dim justTheSubDirectory As String

        For Each oneDirectory As String In My.Computer.FileSystem.GetDirectories(Dir)

            Try
                justTheSubDirectory = My.Computer.FileSystem.GetName(oneDirectory)

                Dim dirTest As New DirectoryInfo(oneDirectory)
                Dim dSecurity As DirectorySecurity = dirTest.GetAccessControl()
                Dim bIsProtected As Boolean = dSecurity.AreAccessRulesProtected

                If (bIsProtected = False) Then
                    If (fromnode Is Nothing) Then
                        newDirectory = TreeView1.Nodes.Add(justTheSubDirectory)
                    Else
                        newDirectory = fromnode.Nodes.Add(justTheSubDirectory)
                    End If

                    BuildDirectoryTree(newDirectory, My.Computer.FileSystem.CombinePath(Dir, justTheSubDirectory))

                End If
            Catch ex As Exception

                MessageBox.Show("Message: " + ex.Message.ToString())

            End Try
        Next
    End Sub

The messagebox displays: "Attempted to perform an unauthorized operation", which is nice and all; but what should I look for in the code to work around that.

Thanks again for the help

I should clarify that the code above works; but it is too restrictive and ignores directories only listed as "Read-Only" like the "Document and Settings" folder and subfolders, which I need to be able to access.
 
I should point out that if you don't have access to a folder, then neither does your app. That's something that's enforced by the OS. The way around that would be to run your app as an administrator every time it runs, I think there's a way for your app to request admin elevation upon startup automatically so if it's running from an admin account all you'd have to do is click allow on the UAC prompt (or type in an admin account's name and password if you're running it on a non-admin account) but I don't know how to do that (yet)
 
Using a Try/Catch is the only way I know of handling that situation since you wont know until the program tries to access it.
 
Try/Catch is probably the best approach, checking permissions can get fairly complex. Here is a sample that show basic exploration of the rules:
VB.NET:
Imports System.Security.Principal
Imports System.Security.AccessControl
VB.NET:
Debug.WriteLine("---")
Dim d As String = "c:\system volume information"
'd = "c:\temp"
Dim acl As DirectorySecurity = IO.Directory.GetAccessControl(d)
Dim rules As AuthorizationRuleCollection = acl.GetAccessRules(True, True, GetType(NTAccount))
Dim identity As WindowsIdentity = WindowsIdentity.GetCurrent
Dim principal As New WindowsPrincipal(identity)
For Each rule As FileSystemAccessRule In rules
    Dim acc As NTAccount = CType(rule.IdentityReference, NTAccount)
    Dim sid As SecurityIdentifier = CType(acc.Translate(GetType(SecurityIdentifier)), SecurityIdentifier)
    Debug.WriteLine(acc.Value & If(principal.IsInRole(sid), " (IsInRole)", ""))
    Debug.WriteLine(rule.AccessControlType.ToString) '.Allow or .Deny
    Debug.WriteLine(rule.FileSystemRights.ToString) 'flags
    Debug.WriteLine("")
Next
"System Volume Information" is a common no-access directory, try "Temp" or similar folder also to see there can be many rules that applies, both when it comes to account, allow/deny, and FileSystemRights.
 
Back
Top