文件夹权限?

3

这是我用于设置文件夹权限的函数代码:

 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

现在,当我使用 AddFileSecurity("D:\Protect", "UserUser", "FullControl") 锁定文件夹后,我无法解锁文件夹!

请问我该如何解锁这个文件夹呢?

谢谢!


你的 power 参数应该是一个枚举而不是一个字符串。 - SLaks
但是当我调用函数RemoveFileSecurity("D:\Protect", "UserUser", "FullControl")时,我可以访问D:\Protect,这就是我想要的,但现在当我需要将此文件夹的访问权限返回给UserUser时,我不知道该怎么做! - Comii
1个回答

2

您的AddFileSecurity命名正确,但是您的RemoveFileSecurity实际上并没有删除任何内容,而是拒绝访问。在AddFileSecurity中,您应该调用删除该用户的任何Deny条目,可能是RemoveAccessRuleAll


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接