More Security Info on a directory

Posted on 2007-07-23
Last Modified: 2012-06-21
Using VB2005.
Thanks to some previous help I got here, I have working code that gets security information on a supplied directory. A sample of the code is below.
There are two things I can't figure out:
1) If one clicks on the Advanced button when viewing security, there are two ticks. "Inherit from parent the permission entries that apply to child objects." and "Replace permissions on all child objects..." How can I read the values of these

2) When changing permissions, there is an option to select "This Folder Only", "Subfolder and Files" etc.
How do I read these values? Its important as a user could have full rights on subfolders, but only read rights on the main folder.


    Dim rowPermissions As FolderRightsRow
      Dim getInfo As System.IO.DirectoryInfo
      getInfo = My.Computer.FileSystem.GetDirectoryInfo(stDirectory)

         Dim col As AuthorizationRuleCollection = getInfo.GetAccessControl.GetAccessRules(True, True, Type.GetType("System.Security.Principal.NTAccount"))

         For Each rule As AuthorizationRule In col
            Dim frule As FileSystemAccessRule = CType(rule, FileSystemAccessRule)

            If (frule.FileSystemRights > 0) And (frule.FileSystemRights <= FileSystemRights.FullControl) Then

               rowPermissions = dtPermissions.NewRow

               rowPermissions.AC_Type = frule.AccessControlType.ToString()
               rowPermissions.RightsList = frule.FileSystemRights.ToString()

               If (frule.FileSystemRights And FileSystemRights.FullControl) = FileSystemRights.FullControl Then
                  rowPermissions.FullControl = True
               End If
Question by:jdhackett
    LVL 12

    Accepted Solution

    LVL 41

    Expert Comment

    Unfortuneately, those attributes are not supported by the built-in AccessControl classes.   That means you'll need to venture into the "dark side" of API programming.

    I've got an article that might be of interest... it has downloadable VB.Net source code examples:
    LVL 1

    Author Comment

    graye -thats a nice site you have there.
    I'll try what gbzhhu suggests first, and come back when I've checked that out.
    LVL 1

    Author Comment

    Right, here's what I figured out so far for the values.

    0: This is the default.
    1: NoPropagateInherit. Set if 'Apply there Permissions to objects and/or containters within this container only' is ticked.
    2: InheritOnly. Indicates permissions on Subfolder and Files only. Or Files only. Or Subfolders only. Need to check InheritanceFlags for more info.
    3: NoPropagateInherit and InheritOnly are set.

    True means that "Inherit from parent the permission entries that apply to child objects."  is ticked
    False means its not ticked.

    0: ThisFolderOnly is selected
    1: ContainerInherit. Subfolders only is selected. Or This Folder and Subfolder
    2: ObjectInherit. Files Only. Or This Folder and Files.
    3: Default. 'This Folder, Sub Folder and Files' is selected.

    I didn't find 'Replace Permissions on all child objects'. This flag doesn't stay set, you just tick it and click apply. Any ideas how I would that through code?
    LVL 41

    Expert Comment

    'Replace Permissions on all child objects' isn't an attribute... its an action.  (just like "delete" isn't an attribute of a file)
    LVL 12

    Assisted Solution

    Check out the DirectoryObjectSecurity class.  It has a method called SetAccessRuleProtection (inherited from ObjectSecurity base class)

    Read the bottom of this page

    Note multiline URL!
    LVL 1

    Author Comment

    Ok, reckon thats everything I need. Thanks lads.
    LVL 12

    Expert Comment

    You are very welcome

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now