首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >文件夹权限?

文件夹权限?
EN

Stack Overflow用户
提问于 2010-01-11 23:29:00
回答 1查看 4.5K关注 0票数 3

下面是我用来设置文件夹权限的函数代码:

代码语言:javascript
复制
 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")锁定文件夹时,之后我无法解锁文件夹!

如何解锁此文件夹?

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-01-12 00:53:30

你的AddFileSecurity的名字是正确的,但是你的RemoveFileSecurity实际上并没有删除任何东西,相反,它的拒绝访问。在AddFileSecurity中,您应该添加一个调用来删除该用户的所有Deny条目,可能是RemoveAccessRuleAll

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2042754

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档