2010-01-11 5 views
3

Ceci est le code pour la fonction que j'utilise pour définir la permission du dossier:Autorisation de dossier?

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 

Maintenant, quand BLOQUEES dossier avec AddFileSecurity ("D: \ Protect", "USERUSER", "FullControl"), après que je ne peut pas déverrouiller le dossier!

Comment puis-je déverrouiller ce dossier?

Merci!

+0

Votre paramètre 'power' devrait être une énumération au lieu d'une chaîne. – SLaks

+0

Mais quand j'appelle la fonction RemoveFileSecurity ("D: \ Protect", "UserUser", "FullControl") je peux accéder à D: \ Protect, et c'est ce que je ne veux pas mais maintenant quand je dois retourner l'accès sur ce dossier UserUser je ne sais pas comment faire ça! – Comii

Répondre

2

Votre AddFileSecurity est correctement nommé mais votre RemoveFileSecurity ne supprime pas vraiment quoi que ce soit, au lieu qu'il nie accès. Dans AddFileSecurity, vous devez ajouter un appel pour supprimer toutes les entrées Deny pour cet utilisateur, probablement RemoveAccessRuleAll.