Willkommen bei dotnet-snippets.de! Snippet hinzufügen Login Registrieren
Snippets in der Datenbank: 1563 | Anzahl registrierter User: 1896 | Besucher online: 149
Hauptmenü
Home
Top Ten
Zufälliger Snippet
FAQs
.NET Community
dotnet-forum.de
dotnet-kicks.de
Social

RSS Feeds
Rss Alle Snippets
Rss C#
Rss VB.NET
Rss C++
Rss ASP.NET
Partner
Member of Microsoft Community Leader/Insider Program (CLIP)

NTFS Berechtigungen auf einen Ordner setzen


Autor: Crazy Jurojin
Sprache: VB.NET
Bewertung: 1,8
(1 Bewertung)
Anzahl der Aufrufe: 5817
  
Kick it on dotnet-kicks.de  

Beschreibung:

Kleine Funktion um NTFS Berechtigungen auf einen Ordner zu setzen.


Mein erster Snippet den ich poste, hoffe das passt alles.


Abgelegt unter: NTFS, Berechtigungen, Permissions.



Visual Basic
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
Sie haben Fragen zu diesem Snippet oder brauchen Hilfe bei der .NET Entwicklung?
Freundliche und kompetente Entwickler helfen Ihnen gern weiter im Forum für .NET Entwicklung.



Kommentare:
(Zum Schreiben von Kommentaren bitte anmelden.)



Diese Snippets könnten für Sie interessant sein:

schlecht sehr gut
1 2 3 4 5 6 7 8 9 10
Nur angemeldete User können Snippets bewerten.