This is some code that I was playing with, it is in vb.net but you might be able to convert it
it is not my tidiest code but it basically adds the current user to the ACL for a folder and sets ' administrators ' as the owner, I had some problems setting the owner before I gave permissions
-
-
Imports System.IO
-
Imports System.Security.Permissions
-
Imports System.Security.Principal
-
-
-
Public Class Form1
-
-
-
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
-
Dim self = System.Security.Principal.WindowsIdentity.GetCurrent()
-
Dim Rulz = New System.Security.AccessControl.FileSystemAccessRule(self.Name, System.Security.AccessControl.FileSystemRights.FullControl, System.Security.AccessControl.AccessControlType.Allow) ' create ACL
-
Dim mydir As New DirectoryInfo("c:\test")
-
Dim vals As System.Security.AccessControl.FileSystemAccessRule
-
Dim x = mydir.GetAccessControl()
-
Dim permlists = x.GetAccessRules(True, True, GetType(System.Security.Principal.NTAccount))
-
Dim nt As New System.Security.Principal.NTAccount("", "Administrators")
-
Dim gr As System.Security.Principal.IdentityReference = nt
-
-
Dim tempown = x.GetOwner(GetType(System.Security.Principal.NTAccount)) ' check current owner
-
MsgBox(tempown.Value)
-
-
x.AddAccessRule(Rulz) ' add ACL to X
-
-
mydir.SetAccessControl(x) ' set access control oin folder using x
-
-
x.SetOwner(nt) ' set owner for folder to administrators
-
-
mydir.SetAccessControl(x) ' set access control oin folder using x
-
-
Dim num As Integer = 0
-
-
For Each vals In permlists ' check current permissions
-
-
ListBox1.Items.Add(vals.IdentityReference.Value & " - " & vals.FileSystemRights.ToString)
-
-
num += 1
-
-
Next
-
-
-
-
End Sub
-
End Class
-
-