2010-01-11 269 views
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:\保護」,「USERUSER」,「FullControl」),那我以後無法解鎖文件夾!

我如何解鎖此文件夾?

謝謝!

+0

你的'power'參數應該是一個枚舉而不是一個字符串。 – SLaks 2010-01-11 15:32:47

+0

但是,當我調用函數RemoveFileSecurity(「D:\ Protect」,「UserUser」,「FullControl」)時,我可以訪問D:\ Protect,這是我不會但現在當我需要返回此文件夾訪問UserUser我不知道該怎麼做! – Comii 2010-01-11 15:49:57

回答

2

AddFileSecurity名稱正確,但你的RemoveFileSecurity實際上並沒有刪除任何東西,而是否認訪問。在AddFileSecurity中,您應該添加一個電話以刪除該用戶的任何Deny條目,可能是RemoveAccessRuleAll