Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

C# DirectoryInfo Exceptions on folders...

  • 17-04-2008 11:16pm
    #1
    Registered Users, Registered Users 2 Posts: 2,835 ✭✭✭


    hopefully i'll get my problem across lads its been one of those days....

    I've implemented a breadth-first search algorithm in C# as part of a simple (enough) search window in my program. Basically folders are only added to the Queue if they pass this folder check:
    if (!((dir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
    && !((dir.Attributes & FileAttributes.System) == FileAttributes.System) )
    


    I keep getting an UnauthorizedAccessException on the code below, specifically when i try to get the DirectoryInfo array from the directory.
    
    string searchPath = myQueue.Dequeue(); // only added to the Q if the folder
                                                            //passes the above check (not 
                                                            //a system or hidden folder)
    
    DirectoryInfo dirInf = new DirectoryInfo(searchPath); // Exception thrown
                                                                            // here
    DirectoryInfo[] dirArray = dirInf.GetDirectories();
    

    In the catch block i've got Exception.Message.ToString() popping up in a messageBox and its telling me:
    "Access Denied to :"
    C:\Windows\System32\LogFiles\WMI\RtBackup
    

    Now i know these are folders that i dont have permission to go rooting around in, but is there any way i can skip these folders?

    I thought checking if they were "system" or "hidden" files would be enough, i've tried "FileAttributes.NotContentIndexed" as a check aswell, but i still get the exception.

    Any suggestions appreciated...


Comments

  • Registered Users, Registered Users 2 Posts: 685 ✭✭✭JazzyJ


    You've only checked if the folder is not hidden and not a system folder.

    You'll also need to check if the user running the application has permission to access the folder. The System.Security.Permissions.FileIOPermission class should get you on your way:

    http://msdn2.microsoft.com/en-us/library/system.security.permissions.fileiopermission.aspx


  • Registered Users, Registered Users 2 Posts: 2,931 ✭✭✭Ginger


    What he said! My original post was a bit more convuted


  • Registered Users, Registered Users 2 Posts: 2,835 ✭✭✭StickyMcGinty


    JazzyJ wrote: »
    You've only checked if the folder is not hidden and not a system folder.

    You'll also need to check if the user running the application has permission to access the folder. The System.Security.Permissions.FileIOPermission class should get you on your way:

    http://msdn2.microsoft.com/en-us/library/system.security.permissions.fileiopermission.aspx

    Thanks for the link, but i'm still stuck on this.

    As far as i can see the FileIOPermission is only used to Demand or Deny rights? am i miles off here?

    I've tried to use the DirectorySecurity class to get the AccessControlList for the directory, but so far i cant seem to pull the right attributes from it to determine if I can access it


Advertisement