gpt4 book ai didi

c# - 文件夹权限?

转载 作者:行者123 更新时间:2023-11-30 17:23:40 25 4
gpt4 key购买 nike

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

 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") 锁定文件夹时,之后我无法解锁文件夹!

如何解锁这个文件夹?

谢谢!

最佳答案

您的 AddFileSecurity 已正确命名,但您的 RemoveFileSecurity 实际上并未删除任何内容,而是拒绝访问.在 AddFileSecurity 中,您应该添加一个调用以删除该用户的所有 Deny 条目,可能是 RemoveAccessRuleAll

关于c# - 文件夹权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2042754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com