Kleine Funktion um NTFS Berechtigungen auf einen Ordner zu setzen.
Mein erster Snippet den ich poste, hoffe das passt alles.
Imports System.IO
Imports System.Security.AccessControl
Imports System.Security.Principal
'' Enums erleichtern das setzten von Vererbungen
Public Enum NTFSInherit
SubFoldersAndFiles
ThisFolderSubFoldersAndFiles
ThisFolderAndSubFolders
SubFoldersOnly
ThisFolderAndFiles
FilesOnly
ThisFolderOnly
End Enum
'''''' <summary>
'''''' Setzt NTFS Berechtigung auf den Übergeben Orderpfad
'''''' </summary>
'''''' <param name="sFolderPath">Pfad zum Ordner (Bsp.: C:\Mein\Ordner)</param>
'''''' <param name="sNameAccount">Der Benutzername der in die Berechtigung geschrieben werden soll (Bsp.: Administrator oder UPNName von einer Active Directory Domäme MaxMustert@Test.com)</param>
'''''' <param name="niNTFSInherit">Ob und wie Vererbt werden soll (Enum Werte)</param>
'''''' <param name="fsrPermissions">Die Berechtigungen die gesetzt werden sollen. Mit OR trennen für mehrere</param>
'''''' <param name="actAccess">Allow oder Deny. Zulassen oder Verweigern</param>
'''''' <remarks></remarks>
Public Sub SetFolderNTFSPermissions(ByVal sFolderPath As String, ByVal sNameAccount As String, ByVal niNTFSInherit As NTFSInherit, ByVal fsrPermissions As FileSystemRights, ByVal actAccess As AccessControlType)
Try
Dim dInfo As New DirectoryInfo(sFolderPath)
Dim dSecurity As DirectorySecurity = dInfo.GetAccessControl()
Dim sid As SecurityIdentifier = WindowsIdentity.GetCurrent().User
Dim myrules As Object
myrules = dSecurity.GetAccessRules(True, True, GetType(Security.Principal.NTAccount))
Dim iFlag As New InheritanceFlags
Dim iProg As New PropagationFlags
If niNTFSInherit = NTFSInherit.SubFoldersAndFiles Then
iFlag = InheritanceFlags.ContainerInherit Or InheritanceFlags.ObjectInherit
iProg = PropagationFlags.InheritOnly
ElseIf niNTFSInherit = NTFSInherit.SubFoldersOnly Then
iFlag = InheritanceFlags.ContainerInherit
iProg = PropagationFlags.InheritOnly
ElseIf niNTFSInherit = NTFSInherit.ThisFolderAndFiles Then
iFlag = InheritanceFlags.ObjectInherit
iProg = PropagationFlags.None
ElseIf niNTFSInherit = NTFSInherit.ThisFolderAndSubFolders Then
iFlag = InheritanceFlags.ContainerInherit
iProg = PropagationFlags.None
ElseIf niNTFSInherit = NTFSInherit.ThisFolderSubFoldersAndFiles Then
iFlag = InheritanceFlags.ContainerInherit Or InheritanceFlags.ObjectInherit
iProg = PropagationFlags.None
ElseIf niNTFSInherit = NTFSInherit.FilesOnly Then
iFlag = InheritanceFlags.ObjectInherit
iProg = PropagationFlags.InheritOnly
ElseIf niNTFSInherit = NTFSInherit.ThisFolderOnly Then
iFlag = InheritanceFlags.None
iProg = PropagationFlags.None
End If
Dim AccessRule As New FileSystemAccessRule(sNameAccount, fsrPermissions, iFlag, iProg, actAccess)
dSecurity.ModifyAccessRule(AccessControlModification.Add, AccessRule, True)
dInfo.SetAccessControl(dSecurity)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Kommentare zum Snippet