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

C# DirectoryInfo Exceptions on folders...

Options
  • 18-04-2008 12:16am
    #1
    Registered Users 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 Posts: 683 ✭✭✭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 Posts: 2,931 ✭✭✭Ginger


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


  • Registered Users 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