Folder Resolution?

This is the function code that I use to set permissions on the folder:

 Public Sub AddFileSecurity(ByVal filePath As String, ByVal username As String, ByVal power As String)

        Dim dirinfo As DirectoryInfo = New DirectoryInfo(filePath)

        Dim dirsecurity As DirectorySecurity = dirinfo.GetAccessControl()

        Select Case power

            Case "FullControl"

                dirsecurity.AddAccessRule(New FileSystemAccessRule(username, FileSystemRights.FullControl, InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow))

                dirsecurity.AddAccessRule(New FileSystemAccessRule(username, FileSystemRights.FullControl, InheritanceFlags.None, PropagationFlags.InheritOnly, AccessControlType.Allow))

                dirsecurity.AddAccessRule(New FileSystemAccessRule(username, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly, AccessControlType.Allow))

            Case "ReadOnly"

                dirsecurity.AddAccessRule(New FileSystemAccessRule(username, FileSystemRights.Read, AccessControlType.Allow))

            Case "Write"

                dirsecurity.AddAccessRule(New FileSystemAccessRule(username, FileSystemRights.Write, InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly, AccessControlType.Allow))

                dirsecurity.AddAccessRule(New FileSystemAccessRule(username, FileSystemRights.Write, InheritanceFlags.None, PropagationFlags.InheritOnly, AccessControlType.Allow))

                dirsecurity.AddAccessRule(New FileSystemAccessRule(username, FileSystemRights.Write, InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow))

            Case "Modify"

                dirsecurity.AddAccessRule(New FileSystemAccessRule(username, FileSystemRights.Modify, AccessControlType.Allow))

        End Select

        dirinfo.SetAccessControl(dirsecurity)

    End Sub



Public Sub RemoveFileSecurity(ByVal filePath As String, ByVal username As String, ByVal power As String)

Dim dirinfo As DirectoryInfo = New DirectoryInfo(filePath)

Dim dirsecurity As DirectorySecurity = dirinfo.GetAccessControl()

Select Case power

    Case "FullControl"

        dirsecurity.AddAccessRule(New FileSystemAccessRule(username, FileSystemRights.FullControl, InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Deny))

        dirsecurity.AddAccessRule(New FileSystemAccessRule(username, FileSystemRights.FullControl, InheritanceFlags.None, PropagationFlags.InheritOnly, AccessControlType.Deny))

        dirsecurity.AddAccessRule(New FileSystemAccessRule(username, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly, AccessControlType.Deny))

    Case "ReadOnly"

        dirsecurity.AddAccessRule(New FileSystemAccessRule(username, FileSystemRights.Read, AccessControlType.Deny))

    Case "Write"

        dirsecurity.AddAccessRule(New FileSystemAccessRule(username, FileSystemRights.Write, InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly, AccessControlType.Deny))

        dirsecurity.AddAccessRule(New FileSystemAccessRule(username, FileSystemRights.Write, InheritanceFlags.None, PropagationFlags.InheritOnly, AccessControlType.Deny))

        dirsecurity.AddAccessRule(New FileSystemAccessRule(username, FileSystemRights.Write, InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Deny))

    Case "Modify"

        dirsecurity.AddAccessRule(New FileSystemAccessRule(username, FileSystemRights.Modify, AccessControlType.Deny))

End Select

dirinfo.SetAccessControl(dirsecurity)

End Sub

Now when I lock the folder with AddFileSecurity ("D: \ Protect", "UserUser", "FullControl"), after that I can not unlock the folder!

How can I unlock this folder?

Thank!

+3
source share
1 answer

Yours is AddFileSecuritycorrectly named, but yours RemoveFileSecuritydoes not actually delete anything; instead, it fails . In AddFileSecurityyou should add a call to delete any entries Denyfor this user, possibly RemoveAccessRuleAll.

+2
source

Source: https://habr.com/ru/post/1728048/


All Articles