none
UnauthorizedAccessException - how to allow full file access

    Question

  • Hi all

    I wrote a little backup utility a few years ago on WinXP in Visual Basic using Visual Studion 2008. The program has been working faultlessly for some time.  However, since the recent spontaneous combustion of my old laptop (!) and upgrade to a new one with Windows 7 on, I have fallen foul of UAC and the improved security.

    Before even comparing the files (which will be done on a separate thread), the program counts the number of directories in order to make the progress bar meaningful.  This is done on the main thread, with the following sub:

     Private Sub CountDirectories(ByVal SourceStr As String)
        If IO.Directory.Exists(SourceStr) Then
          For Each Directory As IO.DirectoryInfo In New IO.DirectoryInfo(SourceStr).GetDirectories
            DirCount += 1
            CountDirectories(Directory.FullName)
          Next
        End If
      End Sub
    

    Unfortunately, the sub fails at  'C:\Users\Nick Elliott\AppData\Local\Application Data'.  I'm aware that I could just enclose the sub in a Try/Catch block and miss out the protected folders, but since the program is designed to backup my computer, that isn't really going to solve the problem.  Presumably there must some way to allow the program full file access rights?  I have no problem accepting a UAC dialog each time the program is run.

    I have tried requireAdministrator and highestAvailable in the application manifest, and I also played around with FileIOPermission objects, although I didn't really understand how they work (and the help documentation for those doesn't really explain how to put them to use..)

    I would really appreciate any help you may be able to offer.

     

    Many thanks in advance

     

    Nick

    Tuesday, November 02, 2010 3:00 PM

Answers

All replies

  • Hi Nickelliott,

    Thanks for your post!

     

    Please refer this post, it would be helpful for your question:

     

    Why do I get an Access Denied error when trying to open a folder?

    http://www.jimmah.com/vista/Security/junctions.aspx


    Please feel free to let me know if you have any further issues, thanks!



    Happy Coding:)
    Wayne Ye - Senior Software Development Engineer
    Personal Website:  http://WayneYe.com

    • Proposed as answer by WayneYe Wednesday, November 03, 2010 3:42 AM
    • Marked as answer by Nickelliott Wednesday, November 03, 2010 7:13 PM
    Wednesday, November 03, 2010 3:42 AM
  • Hi Wayne

    Thanks for your reply.

    The post you refer to is talking about junctions, but as far as I can see, 'C:\Users\Nick Elliott\AppData\Local\Application Data' is the real location, not the junction, and that is the folder the program is failing on.

    I think the program is failing due to the folder being somehow protected, and my program not having sufficient priveleges to access it.

    Any other suggestions or thoughts would be welcome.

     

    Nick

    Wednesday, November 03, 2010 10:25 AM
  • Hi Nickelliott,

     

    I'm not sure how could you say the "Application Data" is "real location, not junction", in fact it is.

     

    Junction points on Windows Vista/7/2008  is intended to provide "backward compatibility", for more details, I suggest you look at this MSDN article:

     

    Junction points

    http://msdn.microsoft.com/en-us/library/bb968829(VS.85).aspx 


    Also reference:

    NTFS junction points http://en.wikipedia.org/wiki/NTFS_junction_point


    Please feel free to let me know if you have any further issues, thanks!



    Happy Coding:)
    Wayne Ye - Senior Software Development Engineer
    Personal Website: http://WayneYe.com

    Wednesday, November 03, 2010 2:55 PM
  • ???

     

    Well, as I understand the article you linked to, in Vista (and Win 7) there are new locations for some of the standard folders, but to provide backwards compatibility, junctions have been provided from the old location to the new..

    And, according to the same article, what used to be \Documents and Settings\$USER$\Local Settings\Application Data in XP has now become a junction which points to \Users\$USER$\AppData\Local.  So the 'old' location is a junction, the new location is 'real'.

    But it is the latter location I can't access, which according to the article is the 'real' location.

    Perhaps I have totally misunderstood somehow.

     

    N

    Wednesday, November 03, 2010 3:11 PM
  • Hi Nickelliott,

    Please don't be confused, one thing you can try is, when you try to manually "open"

    %systemdrive%\Documents and Settings\

    or

    C:\Users\{user name}\AppData\Local\Application Data

    You will see a Windows popup warning windows said "Access Denied".

    As a conclusion, I suppose it is time to apply an update for your application and facilitate it has capability with Win7, isn't it:)


    Please feel free to let me know if you have any further issues, thanks!



    Happy Coding:)
    Wayne Ye - Senior Software Development Engineer
    Personal Website: http://WayneYe.com

    Wednesday, November 03, 2010 3:33 PM
  • OK mate, thanks for taking the time to reply, but I'm just not understanding what you're getting at.

    If I try to open 'C:\Documents and Settings' I get the error message Access to the path 'C:\Documents and Settings' is denied.

    If I try to open 'C:\Users\Nick Elliott\AppData\Local\Application Data' I get the message Access to the path 'C:\Users\Nick Elliott\AppData\Local\Application Data' is denied.

    So from this, I conclude that the program isn't failing whilst trying to open one of these legacy file locations; it's failing whilst trying to open the application data directory, which other posts on this and other forums lead me to believe is protected. So I'm trying to allow my program access to protected directories.

     

    Can we agree that \Documents and Settings\$USER$\Local Settings\Application Data is a junction..

    And that it points to \Users\$USER$\AppData\Local, which is a real directory?  After all, they can't both be junctions!  The real directory must exist somewhere.

     

    Nick

    Wednesday, November 03, 2010 5:49 PM
  • In fact Wayne, if you're still with me on this one, let me ask 2 direct questions..

    1) Is 'C:\Users\Nick Elliott\AppData\Local\Application Data' a real folder or a junction?

    2) If it's a junction, where are files which I copy to it actually located on the drive?  If it is a real folder, how do I allow my program read/write access to it?

     

    Many thanks for trying to help!

     

    Nick

    Wednesday, November 03, 2010 5:58 PM
  • Hold on Wayne, I think I've finally caught up!  :-)

     

    Doing 'dir /a' on 'C:\Users\Nick Elliott\AppData\Local\' showed 'Application Data' to be a junction to '/AppData', so that's ok.

     

    [edit out secondary question]

    This is the code I ended up with:

    Private Sub CountDirectories(ByVal SourceStr As String)
        If IO.Directory.Exists(SourceStr) Then
          For Each IndividualDirectory As IO.DirectoryInfo In New IO.DirectoryInfo(SourceStr).GetDirectories
            DirCount += 1
            Dim attributes As FileAttributes
            attributes = File.GetAttributes(IndividualDirectory.FullName)
            If Not ((attributes And FileAttributes.ReparsePoint) = FileAttributes.ReparsePoint) Then                 CountDirectories(IndividualDirectory.FullName)
            End If
          Next
        End If
      End Sub
    I would not have got to that without you, so thanks for perservering!


    Nick

    Wednesday, November 03, 2010 6:24 PM
  • Hi Nickelliott

    Congratulations!  You finally get it:)




    Happy Coding:)
    Wayne Ye - Senior Software Development Engineer
    Personal Website:   http://WayneYe.com
    Thursday, November 04, 2010 3:17 AM