none
Access to the path 'C:\Users\..\My Documents\' is denied

    Question

  • I'm trying to collect directories and add to TreeView. In Windows 7, there is a "Documents" folder under Libraries which is basically linked to "My Documents" under "Users". When I click  "Documents" in the TreeView, I can get a list of all subdirectories by using

                Dim dirs As String() = Directory.GetDirectories(strDir)
    
    

    However, when I click "My Documents", the access is denied. What's the difference? How to solve the problem. I can't remove "My Documents" from the TreeView nodes list because WinXP users need it.

    Another question is that is there a simple way to not include system directories, such as "Windows", "Application Data", ...

    Thank you.

     

    Wednesday, November 23, 2011 7:27 PM

Answers

  • That is a "Junction Point" which is like a Linux symlink, on my Windows 7 computer I have 2 "My Documents" - One is a folder and is accessible, while the other looks like a shortcut and is not.

    If you are enumerating all directories and subdirectories, just use a Try/Catch/End Try to avoid the errors killing your program

    Here is a snip from a TreeView I did a while back - this code is pretty crummy(slow as cold molasses), but the "Access Denied" errors are handled.

        Private Sub PopulateTreeView(ByVal dir As String, ByVal parentNode As TreeNode)
            Dim folder As String = String.Empty
            Dim fileDetail As IO.FileInfo
    
            Try
                'Add the files to treeview
                Dim files() As String = IO.Directory.GetFiles(dir)
                If files.Length <> 0 Then
                    Dim fileNode As TreeNode = Nothing
                    For Each file As String In files
                        fileDetail = My.Computer.FileSystem.GetFileInfo(file)
                        fileNode = parentNode.Nodes.Add(IO.Path.GetFileName(file) & " - " & ShowFriendlyNumber(fileDetail.Length))
                        TotalFiles += 1
                        TotalNumber += fileDetail.Length
                        If TotalFiles Mod 1000 = 0 Then
                            TextBox1.Text = "Folders = " & TotalFolders & " , Files = " & TotalFiles & " , Bytes - " & ShowFriendlyNumber(TotalNumber)
                            TextBox1.Refresh()
                        End If
                        fileNode.Tag = file
                    Next
                End If
                'Add folders to treeview
                Dim folders() As String = IO.Directory.GetDirectories(dir)
                If folders.Length <> 0 Then
                    Dim folderNode As TreeNode = Nothing
                    Dim folderName As String = String.Empty
                    Dim FullFolderName As String = String.Empty
                    For Each folder In folders
                        folderName = IO.Path.GetFileName(folder)
                        FullFolderName = IO.Path.GetFullPath(folder)
                        folderNode = parentNode.Nodes.Add(folderName & " - " & ShowFriendlyNumber(GetFolderSize(FullFolderName, True)))
                        folderNode.Tag = folder
                        TotalFolders += 1
                        PopulateTreeView(folder, folderNode)
                    Next
                End If
            Catch ex As UnauthorizedAccessException
                parentNode.Nodes.Add("Access Denied")
            End Try
        End Sub
    
    

     

    Note the Try/Catch and here are the results:


    as far as skipping, you can maybe use:

    If dir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) 'do domething
    If dir = Environment.GetEnvironmentVariable("SystemRoot") 'do domething

     

     

    Wednesday, November 23, 2011 9:24 PM

All replies

  • That is a "Junction Point" which is like a Linux symlink, on my Windows 7 computer I have 2 "My Documents" - One is a folder and is accessible, while the other looks like a shortcut and is not.

    If you are enumerating all directories and subdirectories, just use a Try/Catch/End Try to avoid the errors killing your program

    Here is a snip from a TreeView I did a while back - this code is pretty crummy(slow as cold molasses), but the "Access Denied" errors are handled.

        Private Sub PopulateTreeView(ByVal dir As String, ByVal parentNode As TreeNode)
            Dim folder As String = String.Empty
            Dim fileDetail As IO.FileInfo
    
            Try
                'Add the files to treeview
                Dim files() As String = IO.Directory.GetFiles(dir)
                If files.Length <> 0 Then
                    Dim fileNode As TreeNode = Nothing
                    For Each file As String In files
                        fileDetail = My.Computer.FileSystem.GetFileInfo(file)
                        fileNode = parentNode.Nodes.Add(IO.Path.GetFileName(file) & " - " & ShowFriendlyNumber(fileDetail.Length))
                        TotalFiles += 1
                        TotalNumber += fileDetail.Length
                        If TotalFiles Mod 1000 = 0 Then
                            TextBox1.Text = "Folders = " & TotalFolders & " , Files = " & TotalFiles & " , Bytes - " & ShowFriendlyNumber(TotalNumber)
                            TextBox1.Refresh()
                        End If
                        fileNode.Tag = file
                    Next
                End If
                'Add folders to treeview
                Dim folders() As String = IO.Directory.GetDirectories(dir)
                If folders.Length <> 0 Then
                    Dim folderNode As TreeNode = Nothing
                    Dim folderName As String = String.Empty
                    Dim FullFolderName As String = String.Empty
                    For Each folder In folders
                        folderName = IO.Path.GetFileName(folder)
                        FullFolderName = IO.Path.GetFullPath(folder)
                        folderNode = parentNode.Nodes.Add(folderName & " - " & ShowFriendlyNumber(GetFolderSize(FullFolderName, True)))
                        folderNode.Tag = folder
                        TotalFolders += 1
                        PopulateTreeView(folder, folderNode)
                    Next
                End If
            Catch ex As UnauthorizedAccessException
                parentNode.Nodes.Add("Access Denied")
            End Try
        End Sub
    
    

     

    Note the Try/Catch and here are the results:


    as far as skipping, you can maybe use:

    If dir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) 'do domething
    If dir = Environment.GetEnvironmentVariable("SystemRoot") 'do domething

     

     

    Wednesday, November 23, 2011 9:24 PM
  • Thank you, Devon.

    I also use the

    Try
    ...
    Catch 
    ...
    End Try
    


    Your

                parentNode.Nodes.Add("Access Denied")
    
    


    is a good suggestion.

    As for skipping, your suggestion can only skip part of the directories. I define an Array collection of typical directories and then compare them. But it is not a simple way I expect.

    Thank you again.

     

     

     

    Wednesday, November 23, 2011 9:52 PM
  • Hi Fea4you,

    Welcome to the MSDN forum.

    I didn’t find an easier way to a reach your goals. If you find a good idea to resolve your question, please share your solutions and experience here, it will be very beneficial for other community members who have similar questions.

    Have a nice day.


    Mark Liu-lxf [MSFT]
    MSDN Community Support | Feedback to us
    Monday, November 28, 2011 3:01 AM
    Moderator