2010-01-11 8 views
3

이것은 내가 폴더 권한 설정에 사용하는 함수에 대한 코드입니다 : 내가 AddFileSecurity과 폴더를 잠글 때 지금폴더 사용 권한?

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 

를 ("D : \ 보호", "UserUser을", "FullControl"), 그 난 후 폴더를 잠금 해제 할 수 없습니다!

어떻게이 폴더의 잠금을 해제 할 수 있습니까?

감사합니다.

+0

'power' 매개 변수는 문자열 대신 enum이어야합니다. – SLaks

+0

하지만 D : \ Protect에 액세스 할 수있는 RemoveFileSecurity ("D : \ Protect", "UserUser", "FullControl") 함수를 호출하면이 폴더에 대한 액세스를 반환해야 할 때가되었습니다. UserUser 어떻게 해야할지 모르겠다! – Comii

답변

2

는 귀하가 AddFileSecurity가 제대로 이름하지만 RemoveFileSecurity 실제로 대신은 액세스를 거부, 아무것도 제거하지 않습니다. AddFileSecurity에서 해당 사용자의 Deny 항목을 제거하는 전화 번호를 추가해야합니다 (아마도 RemoveAccessRuleAll).