1

I have a program that needs to change the permissions on specified folder. What I must do first is to remove any permission that was given to the folder by inheritance (i.e. remove all actuel permission) and add new permissions for specifics users/groups.

I know I can easily remove a permission for a folder for a specific user when I know that user, but is there a way to wipe all permission so that I can start fresh or do I need to find a way to find all existing permission and then remove them one by one ?

What I need to do more specifically is to create a new folder WITHOUT the inherited permissions and set my own.

To be more specific, I would like to do as though I created the directory then I went into security, advanced and removed the inheritance.

Zoe
  • 27,060
  • 21
  • 118
  • 148
David Brunelle
  • 6,528
  • 11
  • 64
  • 104

4 Answers4

1

Not sure if will do everthing you need but those are the .NET tool

Directory.GetAccessControl

Directory.SetAccessControl

paparazzo
  • 44,497
  • 23
  • 105
  • 176
0

Not strictly in .NET, but you can use the ICACLS program with Process.Start() like this:

Process.Start("icacls MyDir /inheritance:r");
Rafael
  • 2,827
  • 1
  • 16
  • 17
0

So I've been doing some test and what I needed to do is called the SetAccessRuleProtection method from the DirectorySecurity and apply it to my DirectoryInfo.

I tried it and it worked.

David Brunelle
  • 6,528
  • 11
  • 64
  • 104
0

I created a program that does the same thing

Private Sub AddPermisssion(ByVal directories As String)
    Dim AccountingPermFolder As String = directories
    Dim AccountingDI As IO.DirectoryInfo = New IO.DirectoryInfo(AccountingPermFolder) 
    Dim AccountingDS As DirectorySecurity = AccountingDI.GetAccessControl
    AccountingDS.SetAccessRuleProtection(True, False) //True Protect file and False remove Inheritance   
    IO.Directory.SetAccessControl(AccountingPermFolder, AccountingDS)

    AccountingDS.AddAccessRule(New FileSystemAccessRule(Admins, FileSystemRights.FullControl, AccessControlType.Allow))
    AccountingDS.AddAccessRule(New FileSystemAccessRule(AccountGroup, FileSystemRights.FullControl, AccessControlType.Allow))
    AccountingDS.AddAccessRule(New FileSystemAccessRule(SystemAdmin, FileSystemRights.FullControl, AccessControlType.Allow))
    AccountingDI.SetAccessControl(AccountingDS)


End Sub
Zoe
  • 27,060
  • 21
  • 118
  • 148
Jose Ortiz
  • 705
  • 1
  • 9
  • 19